Title: Remove the hyperlinks from a Word document in C#
This example shows how you can remove the hyperlinks from a Word document. When you select text on a web page and paste it into a Word document, the result often contains hyperlinks that you don't want. (Actually after you paste the text, you can open the correction popup and select Keep Text Only to not get the hyperlinks.) This example opens a Word file, removes its hyperlinks, and saves the file.
The program uses Microsoft Word's object library, so you need to include a reference to it. In Solution Explorer, right-click References and select Add Reference. Click the COM tab, select the "Microsoft Word 14.0 Object Library" entry (or whatever version is installed on your computer), and click OK.
When you enter the name of a file and click the Remove Hyperlinks button, the following code executes.
// Remove the document's hyperlinks
private void btnRemoveHyperlinks_Click(object sender, EventArgs e)
Cursor = Cursors.WaitCursor;
// Get the Word application object.
Word._Application word_app = new Word.Application();
// Make Word visible (optional).
word_app.Visible = true;
// Open the Word document.
object missing = Type.Missing;
object filename = txtFile.Text;
object confirm_conversions = false;
object read_only = false;
object add_to_recent_files = false;
object format = 0;
Word._Document word_doc =
ref read_only, ref add_to_recent_files,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref format, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
// Remove the hyperlinks.
object index = 1;
while (word_doc.Hyperlinks.Count > 0)
// Save and close the document without prompting.
object save_changes = true;
word_doc.Close(ref save_changes, ref missing, ref missing);
// Close the word application.
word_app.Quit(ref save_changes, ref missing, ref missing);
Cursor = Cursors.Default;
The code first creates a Word application object. This example makes the server visible so you can see the Word document as it is modified, but you can hide the server if you like.
Next the code opens the Word document. Notice the strange way it must pass parameters to the Documents.Open method. All of the parameters (including the file name) must be passed by reference as object, so the program creates some object variables to hold the values it needs to pass into the method. The special value Type.Missing represents a parameter that should be omitted.
After it opens the file, the program enters a while loop that continues as long as the document contains any hyperlinks. Inside the loop the program uses the document's Hyperlinks collection's get_Item method to get the first hyperlink and calls its delete method. Notice that the get_Item method starts the indexes of the hyperlinks with index 1 not 0.
Finally the code saves the document's changes without prompting the user and then closes the Word application server.
Download the example to experiment with it and to see additional details.