Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-22-2020, 04:24 AM
sakhtar6 sakhtar6 is offline Batch Editing Word documents Mac OS X Batch Editing Word documents Office 2011 for Mac
Novice
Batch Editing Word documents
 
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
  #2  
Old 02-24-2020, 12:19 AM
sakhtar6 sakhtar6 is offline Batch Editing Word documents Mac OS X Batch Editing Word documents Office 2011 for Mac
Novice
Batch Editing Word documents
 
Join Date: Feb 2020
Posts: 4
sakhtar6 is on a distinguished road
Default Error amde and corrected but still not working

I have modifies the code but it still freezes after the do you wish to process other files dialog box!

Code I am using is:

Code:
Public Sub BatchReplaceAllTrial()

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

PathToUse = "Macintosh HD: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 = Dir$(PathToUse & "*.docx")

While myFile <> " "

        'Open document
        
    Set myDoc = Documents.Open(PathToUse & 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
I have looked at multiple peoples posts and this seems to work for them and I cannot seem to get it to do so for me!

Last edited by macropod; 02-24-2020 at 03:47 AM. Reason: Added code tags (again)...
Reply With Quote
  #3  
Old 02-24-2020, 01:30 AM
sakhtar6 sakhtar6 is offline Batch Editing Word documents Mac OS X Batch Editing Word documents Office 2011 for Mac
Novice
Batch Editing Word documents
 
Join Date: Feb 2020
Posts: 4
sakhtar6 is on a distinguished road
Default Macro doing a never ending loop

on reading through various forums it would appear that the macro seems to be doing a never ending loop.

On running the debug it highlights .ReplaceAll = 1 and then .Execute and then Wend.

No idea how to resolve this.

Any help would be great.

Thanks
Reply With Quote
  #4  
Old 02-29-2020, 07:55 PM
eduzs eduzs is offline Batch Editing Word documents Windows 10 Batch Editing Word documents Office 2019
Expert
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

If your code is not running as expected, it is not recommended to use "On Error Resume Next", cause this will prevent you from find the error source.
Reply With Quote
  #5  
Old 03-01-2020, 12:16 AM
macropod's Avatar
macropod macropod is offline Batch Editing Word documents Windows 7 64bit Batch Editing Word documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by eduzs View Post
If your code is not running as expected, it is not recommended to use "On Error Resume Next", cause this will prevent you from find the error source.
That isn't going to help it run correctly. The only issue with it's use in the OP's code is that it isn't followed by an appropriately-located 'On Error Goto 0' at a point from which errors should no longer be skipped over. If the OP was simply to delete 'On Error Resume Next', that would likely cause the code to stop exactly where one wouldn't want it to.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 03-02-2020, 06:17 AM
sakhtar6 sakhtar6 is offline Batch Editing Word documents Mac OS X Batch Editing Word documents Office 2011 for Mac
Novice
Batch Editing Word documents
 
Join Date: Feb 2020
Posts: 4
sakhtar6 is on a distinguished road
Default

What changes to the code would you advise?
Reply With Quote
  #7  
Old 03-02-2020, 02:49 PM
macropod's Avatar
macropod macropod is offline Batch Editing Word documents Windows 7 64bit Batch Editing Word documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

As I don't use a Mac, I can't provide any guidance on how to make your code work in that environment. Simply deleting 'On Error Resume Next' isn't going to help. I'd be inclined to insert:
On Error Goto 0
after:
Documents.Close SaveChanges:=wdPromptToSaveChanges
That might help identify where any relevant errors are occurring.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Editing Word documents Batch applying a macro to remove Header and Footer using Batch Auto Addin Edszx Word VBA 2 05-27-2019 11:16 PM
Editing Word documents through VBA in SharePoint algecan Word VBA 0 09-26-2018 05:23 AM
batch extract all tables in multiple word documents ZaidaBa Word Tables 3 05-08-2017 10:22 PM
Emailed documents locked for editing by someone else jibbs Office 1 09-30-2015 05:12 AM
Batch Editing Word documents Batch create Word documents cdfj Word VBA 6 11-07-2012 01:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft