SOLVED

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

Copper Contributor

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.

12 Replies

@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

To clarify, you would use the same macro, except replace the line
MySent.Font.Color = wdColorRed
with
MySent.Font.Color = wdColorAutomatic

Thank you@Jay 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 :)

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 :)

Unfortunately, Word doesn't store any information about the colors those sentences had before you ran the first macro, so there is no programmable way to restore them. The best way now is to write a macro that locates each long sentence, and displays a list of colors for you to choose the one you want to apply. You would have to know which category each sentence belongs to. The best way to know that is to have a backup copy of the document from before the original macro ran, to which you could refer.

Thank you@Jay Freedman but I don't know how to write such macro and it would be too much work because we are talking about almost 1000 sentences.

If I understood your suggestion correctly, I could then without bothering with macro go from one sentence to another and change color, but this is exactly what I want to avoid as it is to much time-consuming and tedious work.

Another problem is that I had  the option to track changes turned on and now all those sentences are marked as changed, although nothing is changed in them, so the editor will be forced to look into them although nothing is there to look. A horrible situation as it requires too much extra work, which is why I am trying to find a solution.

 

Since I had "Track Changes" on, is there a way to simply discard the changes made that hour or day?

@nata7 I don't think Word will help you here. You can accept or reject all of the changes in the document, or you can accept or reject them one at a time and skip over the ones you want to keep tracking. However, there's no simple way to reject only the changes made during a specific time period. Again, that could be done with a macro, but you would need specific knowledge about VBA and Word's objects.

 

One thing that may be helpful: If tracking of formatting changes is enabled (which I believe it is by default), you can use the Next button on the Review ribbon (in the Changes group) to jump from one tracked change to the next one, and when you come to a red-colored long sentence, just click the Reject button -- that will remove the red and leave the previous color. Yes, it will be tedious, but less so than doing all the recoloring manually.

Thank you@Jay Freedman .

I have over 3000 changes in the Reviewing Pane. I am guessing almost 1000 of them are the problematic changes of long sentences to red color. So, it is really too much work also to look through all the 3000+ changes.

Is it possible to filter all the tracked changes according to date? This way at least I wouldnt have to go through all changes since I would have all the problematic ones listed one after the other.

best response confirmed by nata7 (Copper Contributor)
Solution

@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

Thank you@Jay Freedman , you are so kind, I really appreciate it.

 

I created and run your macro. I entered the date and in the end the message popped up: "0 revisions were rejected" - How come? Could it be possible that there is some error in the macro or in something else - maybe in the date form? I entered the date 10/12/2019 - because this is how dates are displayed in the Review pane of my Word document, but overhere we write dates 12.10.2019.

 

Or, could the problem be that I did it in a copy of that document (to test it if it works)? Since my first experiment with macros turned very bad, I am now very cautious of using it, so I made a copy of my manuscript and run your macro there. Must I do it directly in the original document? In the copy are all the changes recorded in the Review pane and could be accepted or rejected, but those changes were not actually made in that copy (they were made in the original document).

@nata7 VBA is a bit unpredictable about the way it interprets dates entered as strings. It depends mostly on your Windows settings, particularly the Short Date format. I suspect that when you entered 10/12/2013 (as month/day/year, which is the default for US English), the program interpreted it in day/month/year format as 10 December 2019.

 

Try running the macro again and enter the date as 12/10/2019 to see whether it works.

Thank you@Jay Freedman , you are my saviour here! You were right, your macro worked with the date form you suggested.

If someone is reading this who is also in similar predicament, let there be no confusion, this solution works but only if you had "Track Changes" on.

Bless you!

1 best response

Accepted Solutions
best response confirmed by nata7 (Copper Contributor)
Solution

@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

View solution in original post