From: Andy Fish on 1 Mar 2006 06:57 Thanks for your help on this Greg. The word object model is not my strong point so this will be really useful to me Andy "Greg" <gmaxey(a)mvps.org> wrote in message news:1141051364.248159.46440(a)j33g2000cwa.googlegroups.com... > Jonathan (and others) > > Sorry for being troublesome. I think I have managed to make this a bit > more presentable: > <snip>
From: Jonathan West on 14 Mar 2006 10:22
Hi Greg, I had a need for this when doing a batch update on a set of templates. After parameterising the FindReplaceAnywhere macro it worked fine for me, replacing text in a textbox in the first page header of the template. -- Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org "Greg" <gmaxey(a)mvps.org> wrote in message news:1141051364.248159.46440(a)j33g2000cwa.googlegroups.com... > Jonathan (and others) > > Sorry for being troublesome. I think I have managed to make this a bit > more presentable: > > Public Sub FindReplaceAnywhere() > Dim rngStory As Word.Range > Dim pFindTxt As String > Dim pReplaceTxt As String > Dim lngJunk As Long > Dim oShp As Shape > > pFindTxt = InputBox("Enter the text that you want to find.", _ > "FIND") > If pFindTxt = "" Then > MsgBox "Cancelled by User" > Exit Sub > End If > Tryagain: > pReplaceTxt = InputBox("Enter the replacement.", "REPLACE") > If pReplaceTxt = "" Then > If MsgBox("Do you just want to delete the found text?", > vbYesNoCancel) = vbNo Then > GoTo Tryagain > ElseIf vbCancel Then > MsgBox "Cancelled by User." > Exit Sub > End If > End If > 'Fix the skipped blank Header/Footer problem > lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType > ResetFRParameters > 'Iterate through all story types in the current document > For Each rngStory In ActiveDocument.StoryRanges > 'Iterate through all linked stories > Do > SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt > On Error Resume Next > If rngStory.ShapeRange.Count > 0 Then > For Each oShp In rngStory.ShapeRange > If oShp.TextFrame.HasText Then > SearchAndReplaceInStory oShp.TextFrame.TextRange, _ > pFindTxt, pReplaceTxt > End If > Next > End If > On Error GoTo 0 > 'Get next linked story (if any) > Set rngStory = rngStory.NextStoryRange > Loop Until rngStory Is Nothing > Next > End Sub > Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _ > ByVal strSearch As String, _ > ByVal strReplace As String) > With rngStory.Find > .ClearFormatting > .Replacement.ClearFormatting > .Text = strSearch > .Replacement.Text = strReplace > .Execute Replace:=wdReplaceAll > End With > End Sub > Sub ResetFRParameters() > With Selection.Find > .ClearFormatting > .Replacement.ClearFormatting > .Text = "" > .Replacement.Text = "" > .Forward = True > .Wrap = wdFindContinue > .Format = False > .MatchCase = False > .MatchWholeWord = False > .MatchWildcards = False > .MatchSoundsLike = False > .MatchAllWordForms = False > .Execute > End With > End Sub > |