Home > Software engineering >  WORD 2019 Crashes When Using Macro on Large Documents
WORD 2019 Crashes When Using Macro on Large Documents

Time:10-27

I wrote the 1st ever macro.(Windows 10, WORD 2019) I'm trying to find words that start with a capital letter in Standard style and continue to have Italic style. Unfortunately, if I search a document using too many letters in the query - the macro closes (after checking, for example, 1.5 pages) or resets the WORD program.

If I reduce the number of words searched - the macro starts to run longer and longer. With, for example, a search for 1 letter (U) instead of 32 (ABCDEFGHIJKLŁMNOPQRSTUVWXYZĆŚŃŻŹ) - it does not crash the program.

I tried to add

Application.ScreenUpdating = False at the beginning and 
Application.ScreenUpdating = True at the end of the code but it doesn't help much.  

Sub Makro1()

Dim Rng As Range
Selection.MoveRight Unit:=wdCharacter, Count:=2
    
    Selection.find.ClearFormatting
    With Selection.find.Font
        .Bold = False
        .Italic = False
    End With
    Selection.find.Replacement.ClearFormatting
    With Selection.find
         .Text = "U"
        '.Text = "([ABCDEFGHI])"
        '.Text = "([ABCDEFGHI])"
        '.Text = "([JKLŁMNOP])"
        '.Text = "([QRSTUVWX])"
        '.Text = "([YZĆŚŃŻŹ])"
        '.Text = "([ABCDEFGHIJKLŁMNOPQRSTUVWXYZĆŚŃŻŹ])"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With

    Selection.find.Execute

Selection.MoveRight Unit:=wdCharacter, Count:=2

Set Rng = Selection.Range

If Rng.Italic = True Then
MsgBox "FIND"
End

If Rng.Italic = False Then
Call Makro1
End If


End If

Call Makro1

End Sub

CodePudding user response:

You guided me to the solution. The problem was calling the macro in a loop using Call. After adding START: at the beginning (and then the rest of the code) and instead of "Call Macro 1" writing "GoTo START" in two places solved the problem.

CodePudding user response:

you're not finishing your code correctly. When calling a macro from that macro you're creating a loop which never ends. By calling macro1 all the time, the runtime will terminate the Word Application after too many iterations. You probably don't need to loop (which is a wired loop you created there) through the selection.

The Find-Object provides everything you need. To write all capital letters which are the first ones in a word to italic, when it's not bold or underlined, you could use this, for example:

Sub m()
    With ActiveDocument.Content.Find
        .Text = "<[A-Z]"
        .Style = "Standard"
        .Font.Bold = False
        .Font.Underline = False
        .MatchWildcards = True
        .MatchCase = True
        .Replacement.Font.Italic = True
        .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
    End With
End Sub

If you want to set the first capital character of a word to italic, when the secnd character is italic too, you have to loop over all possible words:

Sub m1()
    With ActiveDocument.Content
        With .Find
            .Text = "<[A-Z]*>"
            .MatchWildcards = True
            .MatchCase = True
            .Execute Forward:=True, Wrap:=wdFindStop
        End With
        Do While .Find.Found
            If .Words.Last.Characters(2).Font.Italic = True Then .Font.Italic = True
            .Find.Execute
        Loop
    End With
End Sub

You could specify other letters like you did above if you need/want.

If you need more precise selection criteria, you can search them on the help page: https://learn.microsoft.com/en/office/vba/api/word.find

Good Luck

  • Related