PegDalyPA was telling us:
PegDalyPA nous racontait que :
HELP!
Hi. This last sample may work for my problem, too, but I can't get
it to work. I'm just learning VBA (on my own from the help files) so
please excuse any idiocy in the question.
I'm working in Word 2003 with VBA and I'm trying to delete all shapes
in all headers of my current document IF their assigned name
(assigned by another macro) is LIKE "PowerPlusWaterMarkDraft*".
There could be a number at the end of the name or it could be a
letter and a number, hence the asterisk, e.g. ,
PowerPlusWaterMarkDraft1, PowerPlusWaterMarkDraft1a,
PowerPlusWaterMarkDraft2, etc.
I tried the following code based on the sample, but it didn't work.
Any idea what I'm doing wrong?
Sub MY_DELETE_DRAFT()
' ** DESCRIPTION **
' Disable Draft background in ALL headers
Dim shp As Word.Shape
On Error Resume Next
'Go into Header (I think it can't find shapes in headers unless
cursor is in a header)
Avoid the Selection Object like the pest, especially when dealing with
headers/footers. It is very unreliable and slows down the execution.
In other words, there is no need to have code actually place the cursor in a
header or footer.
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
For Each shp In ActiveDocument.HeaderFooter.ShapeRange
If Selection.ShapeRange.name Like
"PowerPlusWaterMarkDraft*" Then
Selection.ShapeRange.Delete End If
Next
End Sub
Thanks for any help you can provide. I'm losing my mind over this!
It is always better to start a new thread than to latch on an existing one
with a different question, even if it is sort of related.
Meanwhile, for more detailed information on dealing with storyranges and
searching them in documents, see:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
I have adapted that code for your purpose:
'_______________________________________
Option Explicit
'_______________________________________
Public Sub FindReplaceHeaderFooter()
Dim rngStory As Word.Range
Dim pFindTxt As String
Dim lngJunk As Long
pFindTxt = "PowerPlusWaterMarkDraft"
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
Select Case rngStory.StoryType
'All headers, footers...
Case 6, 7, 8, 9, 10, 11
SearchInStory rngStory, pFindTxt
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
'_______________________________________
'_______________________________________
Public Sub SearchInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String)
Dim shpToDelete As Shape
If rngStory.ShapeRange.Count > 0 Then
For Each shpToDelete In rngStory.ShapeRange
If InStr(1, shpToDelete.Name, strSearch) > 0 Then
shpToDelete.Delete
End If
Next
End If
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org