Results 1 to 4 of 4

Thread: [WORD] Macro gets stuck in tables

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    4

    [WORD] Macro gets stuck in tables

    Hello everyone,

    I am having trouble with a macro that is supposed to go through the Word document (Word 2016) and remove all of the yellow highlighting (just yellow), and then show a message box with how many highlights were removed. The issue is that when I have highlighting in a table, the macro gets stuck in an infinite loop and the program has to be closed, losing any unsaved work.

    Code:
    Sub RemoveYellowHighlightingCount()
    Dim myInt As Integer
    myInt = 0
    
    Selection.HomeKey unit:=wdStory
    With Selection.Find
      .ClearFormatting
      .Highlight = True
      Do While (.Execute(Forward:=True) = True) = True
        If Selection.Range.HighlightColorIndex = wdYellow Then
            Selection.Range.HighlightColorIndex = wdAuto
            Selection.Collapse Direction:=wdCollapseEnd
            myInt = myInt + 1
        End If
      Loop
    End With
    
    Select Case myInt
        Case 0
            MsgBox "No yellow highlighted ranges were found."
        Case 1
            MsgBox "One yellow highlighted range was removed."
        Case Is > 1
            MsgBox myInt & " yellow highlighted ranges were removed."
        End Select
    End Sub
    Any help would be appreciated, thanks.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [WORD] Macro gets stuck in tables

    if you want some help, post a sample document that demonstrates the problem
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    4

    Re: [WORD] Macro gets stuck in tables

    I have attached a document with the macro already in it. I would suggest manually walking through the macro, otherwise it will lock up Word. There is a second macro included, which we use to insert in the green Data Pendings.
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [WORD] Macro gets stuck in tables

    i tested your code (as posted above), with the sample doc worked ok, no error, finished correctly

    please note, this was run in an earlier version of word, so some differences might apply. i will try to test again in a later version
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width