0

ISSUE: How to fix a Word VBA FOR EACH subroutine that seeks to insert a comment at each tracked change and, yet, will freeze VBA/Word at certain pages of a document and not other pages.

The below code works on some documents throughout the entire document. Other documents, unfortunately, will freeze at certain locations in the document.

I've F8 stepped through, for example, to find one document would freeze at pages 6 and 7 of a 22 page document. Strangely, however, I could run the code to insert comments at each tracked pages on all other pages.

Dim rev As Revision, txt As String
Dim pgno1 As String
Dim pgno2 As String

Application.ScreenUpdating = False

ActiveDocument.TrackRevisions = False

'check Revisions
For Each rev In ActiveDocument.Revisions
    Select Case rev.Type 
        Case wdRevisionDelete
            txt = Left(rev.Range.Text, 3) 'the deleted text
                rev.Range.Comments.Add Range:=rev.Range, Text:="Pg [#" & pgno1 & "] BLAH " & txt & " [ ... ]“
        Case wdRevisionInsert
            txt = Left(rev.Range.Text, 3) 'the inserted text
                rev.Range.Comments.Add Range:=rev.Range, Text:= "Pg [#" & pgno1 & "] BLAH " & txt & " […]”
 End Select
 Next rev
 ActiveDocument.TrackRevisions = True

GOAL: Insert comment with specific text at each tracked change, throughout entire docxz

ERRORS: No messages. VBA and Word freeze, requiring restart.

braX
  • 11,506
  • 5
  • 20
  • 33
  • What happens if you put a `DoEvents` just before `Next rev`? – Sam Oct 11 '19 at 16:56
  • What's on those pages? How do they differ? Put in some logging so that you can see *where in the code* it's stopping. Please describe "freezing" in more detail - is it possible the code goes into an infinite loop? Will holding down Ctrl+Pause break into and stop the code? – Cindy Meister Oct 11 '19 at 17:55
  • Thanks, Sam! DoEvents was a great suggestion. It exposed the culprit location. Can one small sentence produce disproportionately high numbers of tracked changes? – Colin Miller Oct 11 '19 at 17:55
  • Ha! I was about to send out the "Bat Signal" for Cindy Meister, Graham Mayor, Greg Maxey, and/or Doug Robbins. "Freezing" - Both Word and VBA windows stop responding, which requires restarting Word. F8 helps the most to identify the location of the strange/innumerate tracked changes. Thinking Out Loud: Can the tracked changes be limited to only text and not formatting changes? Should the FOR EACH loop through only wdMainTextStory and not other "story". – Colin Miller Oct 11 '19 at 17:56
  • RE: "What's on those page?" The document has very simple formatting and text. It's akin to a main paragraph with subsections. – Colin Miller Oct 11 '19 at 18:02

0 Answers0