View Single Post
 
Old 02-22-2020, 04:24 AM
sakhtar6 sakhtar6 is offline Mac OS X Office 2011 for Mac
Novice
 
Join Date: Feb 2020
Posts: 4
sakhtar6 is on a distinguished road
Default Batch Editing Word documents

I found a very useful article on batch editing headers and footers in multiple word files in a folder. It was based on a macro for MS Word for Windows. I have modified for mac OS but I cannot get it to work.

I am using Mac OS with Office 2011.

The code I am using is:

Code:
Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = "MacintoshHD:Users:user:Trial"

'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

myFile = "MacintoshHD:users:user:desktop:Trial" & "*.docx"

While myFile <> ""

    'Open document
    Set myDoc = Documents.Open("MacintoshHD:users:user:desktop:Trial" & myFile)

    If FirstLoop Then

        'Display dialog on first loop only

        Dialogs(wdDialogEditReplace).Show

        FirstLoop = False

        Response = MsgBox("Do you want to process " & _
        "the rest of the files in this folder", vbYesNo)
        If Response = vbNo Then Exit Sub

    Else

        'On subsequent loops (files), a ReplaceAll is
        'executed with the original settings and without
        'displaying the dialog box again

        With Dialogs(wdDialogEditReplace)
            .ReplaceAll = 1
            .Execute
        End With

    End If
    
    
    'Close the modified document after saving changes

    myDoc.Close SaveChanges:=wdSaveChanges

    'Next file in folder

    myFile = Dir$()

Wend

End Sub
A dialogue box opens on running asking if I wish to process the rest of the files. If I press yes the existing file closes and word then freezes. I have to do a Force Quit.

Anyone any ideas as to how to proceed?

Any help would be great.

Thanks

Last edited by macropod; 02-22-2020 at 02:35 PM. Reason: Added code tags
Reply With Quote