简体   繁体   中英

word 2013 VBA / Macro crashes

Word 2013 crashes when running a VBA/macro on a document that has comments section enabled. The macro searches for a character and replaces it with "" (blank) in the comments section and main document. This is done using Selection.Find.Execute API. When there is no comments section or no mark up is selected, Word doesn't crash.

This is on Windows 10 and word 2013. The macro works well in Word 2007 and Windows 7.

Reproducible steps:

  1. Create a simple document with one text and add comment.

  2. Insert the sample comment text as below: (add vertically like: A : & (enter) and so on)

     A: & B: & C: & D: & E: & F: & G: & H: & I: & J: & K: & L: & M: & N: & 
  3. Create macro:

     Sub Test_Macro() ActiveWindow.View.SplitSpecial = wdPaneComments With Selection.Find .Text = "&" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.HomeKey unit:=wdStory If Selection.Find.Execute = True Then 'We found the slash. Delete it. Selection.TypeBackspace End If End Sub 
  4. Execute the Macro ( & will be cleared). Keep on executing again; Word will crash 2-3 times out of 10. (update the comment text accordingly, when all & s are cleared).

I do not see a question in your text, so I can not answer, but I can confirm that Microsoft Word 2013 crashes on ActiveDocument.Content.Find.Execute . I routinely had to work around this by manually exercising the Replace dialog before executing the macro. This worked (almost) every time. Now I found this post that says that it has something to do with Execute running with an empty undo record. I was able to work around this issue by inserting a line of code that adds some text to the last paragraph of the document right after the undo record is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM