One of the common requirements in working with Microsoft Word from .NET is to bold some text. This is often required to draw attention to specific words within the document.
This code example highlights any instances of the word "was". It can be changed to instead highlight any word you desire.
In C#:
// Add to the top of the code file
using Word = Microsoft.Office.Interop.Word;
// Add to a subroutine
Word.Application Wd;
Word.Document doc;
object missingValue = Missing.Value;
// Start Word and get Application object
Wd = new Word.Application();
// Add a new document
doc = Wd.Documents.Add(ref missingValue,ref missingValue,
ref missingValue,ref missingValue );
// Write some text
Wd.Selection.TypeText("Once upon a time there was a document. " +
"The document was fair and fine." +
"The document was short.");
// Bold the specified word
foreach (Word.Range w in doc.Words)
{
if (w.Text.Trim() == "was")
w.Font.Bold = 1;
}
// Save
object fileName = @"test1.docx";
doc.SaveAs(ref fileName,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue,
ref missingValue, ref missingValue,
ref missingValue);
// Close
doc.Close(ref missingValue, ref missingValue, ref missingValue);
doc = null;
Wd.Quit(ref missingValue, ref missingValue, ref missingValue);
// Clean up
// NOTE: When in release mode, this does the trick
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect() ;
In VB:
‘ Add to the top of the code file
Imports Word = Microsoft.Office.Interop.Word
‘ Add to a subroutine
Dim Wd As Word.Application
Dim doc As Word.Document
‘ Start Word and get Application object
Wd = New Word.Application
‘ Add a new document
doc = Wd.Documents.Add
‘ Write some text
Wd.Selection.TypeText("Once upon a time there was a document. " & _
"The document was fair and fine." & _
"The document was short.")
‘ Bold the specified word
For Each w As Word.Range In doc.Words
If (w.Text.Trim() = "was") Then
w.Font.Bold = 1
End If
Next
‘ Save
doc.SaveAs("test1.docx")
‘ Close
doc.Close()
doc = Nothing
Wd.Quit()
‘ Clean up
‘ NOTE: When in release mode, this does the trick
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
In both of these examples, the code starts Word, creates a new Word document, and writes some text into the document. It then loops through the words in the document and bolds any that match "was". You can, of course, change this to any word.
The code then saves the document. Since no directory was found, it defaults to the My Document folder.
Notice the missingValue variable in the C# code that is not in the VB code. VB supports default parameters, but C# does not. So any time a parameter is defined for a Word method, C# must provide it. VB will use the default parameter values.
NOTE: A new feature in C# 4.0 (Visual Studio 2010) allows for default parameters in C# as well, dramatically simplifying the C# code that interacts with Word or Excel.
Enjoy!