Share via

How to batch process this macro?

Anonymous
2022-02-15T03:16:51+00:00

I am hoping to use the below macro (courtesy of Jay Freedman in this link) in batch processing hundreds of files in a folder. How can I do this? I tried using GM's batch processing add-in but I think the add-in's "user defind process" batch process feature needs the code to have a function name. Does anyone have any solutions with or without using GM's batch processing add-in? I'm using Windows OS, Word 2019. Many thanks!

Sub DeleteTwoCharWordsWithExceptions() 

    Dim ExceptDic As Object 

    Dim exceptlist As Variant 

    Dim idx As Long 

    Dim rg As Range 

    Set ExceptDic = CreateObject("Scripting.Dictionary") 

    exceptlist = Split("in|of|to|is|it|on|no|us|at|un|go|an|my|up|me|as|he|we|so|be|by|or|do|if|hi|bi|ex|ok", "|") 

    ' load the dictionary with words from exceptlist 

    For idx = 0 To UBound(exceptlist) - 1 

        ExceptDic.Add exceptlist(idx), exceptlist(idx) 

    Next idx 

    Set rg = ActiveDocument.Range 

    With rg.Find 

        .MatchWildcards = True 

        .Text = "<??>[ .,\?\)^13]" 

        .Format = False 

        .Forward = True 

        .Wrap = wdFindStop 

        While .Execute 

            If Not ExceptDic.Exists(Trim(rg.Text)) Then 

                rg.Text = "" 

                rg.Collapse wdCollapseEnd 

                DoEvents 

            End If 

        Wend 

    End With 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2022-02-15T12:40:01+00:00

To use the macro (which I have not tested) with my batch add-in, it would have to be edited as follows:

Function DeleteTwoCharWordsWithExceptions(oDoc As Document) As Boolean
Dim ExceptDic As Object
Dim exceptlist As Variant
Dim idx As Long
Dim rg As Range

On Error GoTo Err\_Handler  
Set ExceptDic = CreateObject("Scripting.Dictionary")  
exceptlist = Split("in|of|to|is|it|on|no|us|at|un|go|an|my|up|me|as|he|we|so|be|by|or|do|if|hi|bi|ex|ok", "|")  

' load the dictionary with words from exceptlist  
For idx = 0 To UBound(exceptlist) - 1  
    ExceptDic.Add exceptlist(idx), exceptlist(idx)  
Next idx  

Set rg = oDoc.Range  
With rg.Find  
    .MatchWildcards = True  
    .Text = "&lt;??&gt;[ .,\?\)^13]"  
    .Format = False  
    .Forward = True  
    .Wrap = wdFindStop  
    While .Execute  
        If Not ExceptDic.Exists(Trim(rg.Text)) Then  
            rg.Text = ""  
            rg.Collapse wdCollapseEnd  
            DoEvents  
        End If  
    Wend  
End With  
DeleteTwoCharWordsWithExceptions = True  

lbl_Exit:
Exit Function
Err_Handler:
DeleteTwoCharWordsWithExceptions = False
Resume lbl_Exit
End Function

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2022-02-15T06:17:08+00:00

    Try

    Sub DeleteTwoCharWordsWithExceptions()

    Dim ExceptDic As Object

    Dim exceptlist As Variant

    Dim idx As Long

    Dim rg As Range

    Dim myFile As String

    Dim PathToUse As String

    Dim myDoc As Document

    Set ExceptDic = CreateObject("Scripting.Dictionary")

    exceptlist = Split("in|of|to|is|it|on|no|us|at|un|go|an|my|up|me|as|he|we|so|be|by|or|do|if|hi|bi|ex|ok", "|")

    ' load the dictionary with words from exceptlist

    For idx = 0 To UBound(exceptlist) - 1

    ExceptDic.Add exceptlist(idx), exceptlist(idx) 
    

    Next idx

    PathToUse = "C:\Test" 'Modify as required

    myFile = Dir$(PathToUse & "*.doc*")

    While myFile <> ""

    Set myDoc = Documents.Open(PathToUse & myFile) 
    
    Set rg = myDoc.Range 
    
    With rg.Find 
    
        .MatchWildcards = True 
    
        .Text = "&lt;??&gt;[ .,\?\)^13]" 
    
        .Format = False 
    
        .Forward = True 
    
        .Wrap = wdFindStop 
    
        While .Execute 
    
            If Not ExceptDic.Exists(Trim(rg.Text)) Then 
    
                rg.Text = "" 
    
                rg.Collapse wdCollapseEnd 
    
                DoEvents 
    
            End If 
    
        Wend 
    
    End With 
    
    myDoc.Close SaveChanges:=wdSaveChanges 
    
    myFile = Dir$() 
    

    Wend

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2022-02-16T06:31:58+00:00

    Hi Graham,

    Thank you for your code! I tried it out but it didn't work straight away. It initially only processed the first file in the folder, and got an error message saying "Unable to execute the process you defined. Ensure the procedure named "DeleteTwoCharWordsWithExceptions" exists in the Normal template and try again." I created a docm template file in the Startup folder and tried to run the code from there and it worked ok! My problem is now solved, thank you again!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-02-16T05:47:42+00:00

    Hi Doug, thank you for your reply, I inserted your code as a module in VBA editor, added the folder address where it says "C:\Test" 'Modify as required", and clicked on run macro, but nothing happened. It doesn't look like it's processing anything.

    Was this answer helpful?

    0 comments No comments