Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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](<xref:Microsoft.Office.Interop.Word.Documents.Add%2A>) 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"expectederrors": [
{
"file": "docs/csharp/advanced-topics/interop/snippets/NamedAndOptional/WordProgram.cs",
"line": 8,
"line": 5,
"column": 24,
"error": "CS0234"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +62,9 @@ static void DisplayInWord()
range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1,
Format: Word.WdTableFormat.wdTableFormatElegant);
//</Snippet11>

// Make Word visible after all operations are complete
wordApp.Visible = true;
}
}
}
Expand All @@ -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;
}
//</Snippet6>

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;
Expand All @@ -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;
}
}
}
Loading