2

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?

Ivan Rubanau
  • 374
  • 2
  • 12
  • This is likely related to the document in question and not a general issue. It might be that there is an issue with a particular formatting or Word feature being used in the document. You can try to narrow things down be removing things from the document until the macro works. – Dirk Vollmar Nov 09 '16 at 14:31
  • Thanks for idea, but I just tried to do it with a new empty document and It crashed either. – Ivan Rubanau Nov 09 '16 at 14:39

1 Answers1

5

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
Winter
  • 3,894
  • 7
  • 24
  • 56
Thach
  • 66
  • 1
  • 2