Search code examples
c#vbams-wordvstoword-2013

Word 2013 Crash on Find.Execute


I'm using VSTO ThisAddIn and Word 2013.

I want to replace all occurences of the one string with another using Find.Execute and mark this action with custom undo message. The code is:

application.UndoRecord.StartCustomRecord("Some Message");

var find = application.ActiveDocument.Content.Find;
find.Execute("string to be replaced"
  , ReplaceWith: "second string"
  , Replace: WdReplace.wdReplaceAll);

application.EndCustomRecord();

So I have a Word Crash during this code run. If I change Replace: WdReplace.wdReplaceAll to Replace: WdReplace.wdReplaceOne, I'll have word working, but I need to replace all occurrences.

Any suggestions/workarounds?


Solution

  • It's some issue with Word when you have an empty custom undo and do a replace all. I just ran into this issue today.

    This page explains it well.

    Options

    1. Replace one, then replace all
    2. Workaround by making a change to the doc before replace all, then undoing that change
    3. remove the custom undo