diff --git a/docs/csharp/advanced-topics/interop/how-to-use-named-and-optional-arguments-in-office-programming.md b/docs/csharp/advanced-topics/interop/how-to-use-named-and-optional-arguments-in-office-programming.md index b09dfda8d5a22..ad192acb64a5c 100644 --- a/docs/csharp/advanced-topics/interop/how-to-use-named-and-optional-arguments-in-office-programming.md +++ b/docs/csharp/advanced-topics/interop/how-to-use-named-and-optional-arguments-in-office-programming.md @@ -38,6 +38,9 @@ In **Solution Explorer**, right-click the *Program.cs* file and then select **Vi In the `Program` class in *Program.cs*, add the following method to create a Word application and a Word document. The [Add]() method has four optional parameters. This example uses their default values. Therefore, no arguments are necessary in the calling statement. +> [!NOTE] +> To avoid COM threading and timing issues that can cause exceptions like "The message filter indicated that the application is busy" (HRESULT 0x8001010A), the Word application is kept invisible during operations and only made visible after all operations are complete. + :::code language="csharp" source="./snippets/NamedAndOptional/wordprogram.cs" id="Snippet6"::: Add the following code at the end of the method to define where to display text in the document, and what text to display: diff --git a/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/snippets.5000.json b/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/snippets.5000.json index 7aa30d9f5c11d..74059af081788 100644 --- a/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/snippets.5000.json +++ b/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/snippets.5000.json @@ -3,7 +3,7 @@ "expectederrors": [ { "file": "docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/WordProgram.cs", - "line": 8, + "line": 5, "column": 24, "error": "CS0234" } diff --git a/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/wordprogram.cs b/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/wordprogram.cs index 869010da0a10b..9ca8a369f85a9 100644 --- a/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/wordprogram.cs +++ b/docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/wordprogram.cs @@ -19,7 +19,8 @@ static void Main(string[] args) static void DisplayInWord() { var wordApp = new Word.Application(); - wordApp.Visible = true; + // Keep Word invisible during operations to avoid COM threading issues + wordApp.Visible = false; // docs is a collection of all the Document objects currently // open in Word. Word.Documents docs = wordApp.Documents; @@ -61,6 +62,9 @@ static void DisplayInWord() range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1, Format: Word.WdTableFormat.wdTableFormatElegant); // + + // Make Word visible after all operations are complete + wordApp.Visible = true; } } } @@ -74,20 +78,25 @@ class Parts static void DisplayInWord() { var wordApp = new Word.Application(); - wordApp.Visible = true; + // Keep Word invisible during operations to avoid COM threading issues + wordApp.Visible = false; // docs is a collection of all the Document objects currently // open in Word. Word.Documents docs = wordApp.Documents; // Add a document to the collection and name it doc. Word.Document doc = docs.Add(); + + // Make Word visible after operations are complete + wordApp.Visible = true; } // static void VS2008() { var wordApp = new Word.Application(); - wordApp.Visible = true; + // Keep Word invisible during operations to avoid COM threading issues + wordApp.Visible = false; // docs is a collection of all the Document objects currently // open in Word. Word.Documents docs = wordApp.Documents; @@ -101,6 +110,9 @@ static void VS2008() Word.Range range = doc.Range(ref n, ref n); range.InsertAfter("Testing, testing, testing. . ."); + + // Make Word visible after operations are complete + wordApp.Visible = true; } } }