From 6480bd5a94f374c6e09030245f23b8a69d357a0a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 17:59:51 +0000 Subject: [PATCH 1/3] Initial plan From e5a10a1a5503141c6b4c36ba75a3e90a1da7cac1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 18:07:19 +0000 Subject: [PATCH 2/3] Fix COM threading issue in Office interop example Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- ...optional-arguments-in-office-programming.md | 3 +++ .../snippets/NamedAndOptional/wordprogram.cs | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) 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/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; } } } From 84eaaa89098184a1989db2ed144dce4eb98929a7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 2 Jul 2025 13:37:51 +0000 Subject: [PATCH 3/3] Update error location in snippets.5000.json from line 8 to line 5 Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../interop/snippets/NamedAndOptional/snippets.5000.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" }