Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2004

Tip: Looking for answers? Try searching our database.

Find/Replace macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alldreams - 28 Feb 2004 17:44 GMT
Could you give me a basic example of a find/replace macro
for a word document?  I need to do a find/replace for
about 10 uniques values, the uniques values change by
adding 9 to each.  I need to do it about 20-50 different
documents.

Thanks in advance.
Word Heretic - 28 Feb 2004 23:20 GMT
G'day "Alldreams" <anonymous@discussions.microsoft.com>,

check out the dozens of examples and my skeleton on www.editorium.com
in the free back issues of the excellent free Editing with Word
newsletter.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)

Alldreams reckoned:

>Could you give me a basic example of a find/replace macro
>for a word document?  I need to do a find/replace for
[quoted text clipped - 3 lines]
>
>Thanks in advance.
Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS - 29 Feb 2004 07:59 GMT
Here's something that should help (makes use of some code supplied by Peter
Hewett)

Public Sub BatchReplaceAnywhere()
Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim rngstory As Word.Range
Dim FindText As String
Dim Replacement As String
' Get the folder containing the files
With Dialogs(wdDialogCopyFile)
   If .Display <> 0 Then
       PathToUse = .Directory
   Else
       MsgBox "Cancelled by User"
       Exit Sub
   End If
End With
'Close any documents that may be open
If Documents.Count > 0 Then
   Documents.Close Savechanges:=wdPromptToSaveChanges
End If
FirstLoop = True
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
   'Get the text to be replaced and the replacement
   If FirstLoop = True Then
       FindText = InputBox("Enter the text that you want to replace.",
"Batch Replace Anywhere")
       Replacement = InputBox("Enter the replacement text.", "Batch Replace
Anywhere ")
       FirstLoop = False
   End If
   'Open each file and make the replacement
   Set myDoc = Documents.Open(PathToUse & myFile)
   ' Fix the skipped blank Header/Footer problem
   MakeHFValid
   ' Iterate through all story types in the current document
   For Each rngstory In ActiveDocument.StoryRanges
       ' Iterate through all linked stories
       Do
           SearchAndReplaceInStory rngstory, FindText, Replacement
           ' Get next linked story (if any)
           Set rngstory = rngstory.NextStoryRange
       Loop Until rngstory Is Nothing
   Next
   'Close the file, saving the changes.
   myDoc.Close Savechanges:=wdSaveChanges
   myFile = Dir$()
Wend
End Sub
Public Sub SearchAndReplaceInStory(ByVal rngstory As Word.Range, _
                                  ByVal strSearch As String, _
                                  ByVal strReplace As String)
'This routine supplied by Peter Hewett
   Do Until (rngstory Is Nothing)
       With rngstory.Find
           .ClearFormatting
           .Replacement.ClearFormatting
           .Text = strSearch
           .Replacement.Text = strReplace
           .Forward = True
           .Wrap = wdFindContinue
           .Format = False
           .MatchCase = False
           .MatchWholeWord = False
           .MatchAllWordForms = False
           .MatchSoundsLike = False
           .MatchWildcards = False
           .Execute Replace:=wdReplaceAll
       End With
      Set rngstory = rngstory.NextStoryRange
   Loop
End Sub
Public Sub MakeHFValid()
'And this too
   Dim lngJunk As Long
   ' It does not matter whether we access the Headers or Footers property.
   ' The critical part is accessing the stories range object
   lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub

Signature

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested.  Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP

> Could you give me a basic example of a find/replace macro
> for a word document?  I need to do a find/replace for
[quoted text clipped - 3 lines]
>
> Thanks in advance.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.