2
votes

Hi!

I'm working on a VSTO Word Addin that helps the user to replace certain words in a given text. A dialog guides the user through the replacements. Now, when the user replaces the last word I want to display a MessageBox that he is done. There I'm struggeling with a timing issue. The MessageBox appears before the last replacement is visible. Even though, the code line for the replacement is executed before. After clicking the MessageBox away the replacement is made as should be.

Here is the line I use to replace the word:

using Microsoft.Office.Interop.Word;
...
Words wordsText = Globals.ThisAddIn.Application.ActiveDocument.Words;
...
wordsText[wordPos].Text = "[some text]";

And this is my MessageBox displayed later:

MessageBox.Show(this._owner, "[you are done text]", "[title]", MessageBoxButtons.OK, MessageBoxIcon.Information);

I noticed that if I set a breakpoint on the line above in Visual Studio the replacement is made correctly before the appearance of the MessageBox. Can I somehow force changes in Microsoft.Office.Interop.Word.Words to apply instantly? Any other ideas?

1
Are you running the task on a thread? I couldn't reproduce this, works just fine for me. Maybe you can post the full code where you are running the loop over wordPos?etaiso

1 Answers

0
votes

I do have no real solution, just a few ideas:

  • Maybe you can just do System.Windows.Forms.Application.DoEvents();...
  • Or create a new document, delete it again to force Winword to update Application.ActiveDocument.Words...

Hope it helps, Jörg