View Single Post
 
Old 12-02-2016, 04:41 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The following macro allows you to browse to a folder containing the documents you want to process, then replace a given string in all documents in that folder automatically.
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    Call SearchAndReplace(wdDoc)
    wdDoc.Close SaveChanges:=True
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Sub SearchAndReplace(wdDoc As Document)
With wdDoc
  'Your document-specific code goes here
End With
End Sub
Note how the document you want to process is passed to the SearchAndReplace sub. In that sub, you replace references to ActiveDocument with wdDoc. Other changes, too, may be required but, as you haven't posted the relevant code, I can't advise on that.

PS: Asking others to do coding for you that you're being assessed on is called cheating. The only reason I posted the above (hopefully too late to be of use) is because a search of this forum (which you're apparently too lazy to do) would turn up plenty of threads using almost identical code.

Thread closed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]