Forum Discussion

nata7's avatar
nata7
Copper Contributor
Oct 12, 2019

how can I undo a macro in Word? - SOLVED :)

Hi, how can I undo a macro in Word? I added a code to mark long sentences and I run it.This is the code:

Sub Mark_Long()
    Dim iMyCount As Integer
    Dim iWords As Integer
 
    If Not ActiveDocument.Saved Then
        ActiveDocument.Save
    End If
 
    'Reset counter
    iMyCount = 0
 
    'Set number of words
    iWords = 50 ' <==== change as required
 
    For Each MySent In ActiveDocument.Sentences
        If MySent.Words.Count > iWords Then ' <==== play with to look at sentences of varying length
            MySent.Font.Color = wdColorRed
            iMyCount = iMyCount + 1
        End If
    Next
    MsgBox iMyCount & " sentences longer than " & _
      iWords & " words."
End Sub

 

Now all my long sentences (cca. 1000 of them - it is a manuscript) are in red color but I want to undo it so that they are no longer in red - how can I do it? My document is in more colors, so there is no use of simply changing font color.

Also, I don't know why, maybe some bug or error, the Word document closed and reopened itself, so no use of the "undo" action in Word. I need to undo the macro.

I already deleted the macro, so it is not there anymore, but how do I undo what that macro did?

Thank you in advance!

 

UPDATE: solution was provided by generous and great Jay (marked as best response). There are actually two solutions. The first and easy one was  to use a new macro with the same code as old macro but just with a change of line about color. I didn't want the color to change to automatic color, so I needed another solution. That solution is for you too if you had "Track Changes" on because the new macro automatically rejects all those changes on that date.

  • nata7 As I mentioned briefly before, a macro can examine the date on which a tracked change was made, and decide whether to reject that change. The following macro asks you to enter the date that the changes were made. It then looks at each tracked change (all 3000 of them). If the change's date matches the date you entered, and if the font color of that change is red, then the macro rejects that change. Otherwise the change is ignored. The macro counts the number of changes it rejected, and displays the number in the status bar at the bottom of the Word window and in a message box when the macro finishes.

     

    Sub RejectTimedRevision()
    Dim objRev As Revision
    Dim objDate As Date
    Dim strDate As String
    Dim lngCount As Long

    GetDate:
    strDate = InputBox(prompt:="Enter date of changes:", _
    Title:="Reject Changes from Specified Date")
    If strDate = "" Then Exit Sub
    If IsDate(strDate) Then
    objDate = CDate(strDate)
    Else
    MsgBox strDate & " is not a valid date. Try again."
    GoTo GetDate
    End If

    For Each objRev In ActiveDocument.Revisions
    If InStr(objRev.Date, objDate) And _
    (objRev.Range.Font.ColorIndex = wdRed) Then
    objRev.Reject
    lngCount = lngCount + 1
    StatusBar = CStr(lngCount) & " revisions rejected"
    End If
    Next objRev

    MsgBox CStr(lngCount) & " revisions rejected"
    End Sub

  • Haytham Amairah's avatar
    Haytham Amairah
    Silver Contributor

    nata7

     

    Hi,

     

    Unfortunately, the commands that applied through a VBA code cannot be undone!

    So, you have to write a second macro to search for the same sentences and remove their colors!

     

    Regards

    • Jay Freedman's avatar
      Jay Freedman
      MVP
      To clarify, you would use the same macro, except replace the line
      MySent.Font.Color = wdColorRed
      with
      MySent.Font.Color = wdColorAutomatic
      • nata7's avatar
        nata7
        Copper Contributor

        Thank youJay Freedman . Before I do it I wanted to make sure something and ask you: when I do that, will the color than turn black or will it turn to the previous color?

        In my manuscript I have many paragraphs in different colors (with editing each color tells me something - different colors for different editing categories), so now when all the long sentences turned red, I need them to turn to the previous colors, so that I know to which category each edited sentence belongs to.

        I really appreciate your help, it means a lot to me, so bless you 🙂

    • nata7's avatar
      nata7
      Copper Contributor

      Thank you Haytham Amairah .

      Are you sure, because Jay said it could be undone indeed by a new macro with the same but slightly changed code?

      Please could you tell me how to write a second macro to search for the same sentences and remove their colors? Or, did you mean what Jay suggested?

      Sorry, I never used macros before, it is totally new lingo for me, like Escimos language, if you know what I mean, so please have understanding for me.

       

       

      In my manuscript I have many sentences and paragraphs in different colors (with editing each color tells me something - different colors for different editing categories), so now when all the long sentences turned red, I need them to turn to the previous colors, not to black color, so that I know to which editing category each edited sentence belongs to.

      I really appreciate your help, it means a lot to me, so bless you 🙂

Resources