Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRemove personal information from a Word file in VB .NET
DescriptionThis example shows how to remove personal information from a Word file in VB .NET
KeywordsWord, Microsoft Word, Office, Microsoft Office, personal information, VB .NET
CategoriesOffice, Miscellany
 
This program requires a reference to the Word object model. Select Project/Add Reference. Click the COM tab and select "Microsoft Word 11.0 Object Library" (or whatever version you have on your system).

When you click the Open button, the program creates a Word server and opens the document. It sets the document's RemovePersonalInformation property to True and saves the file. This removes the personal information from the file and makes the file not save that information in the future.

A slightly odd thing happens in this code. The call to Open takes a parameter declared as an Object. If you try to pass in txtFile.Text, Visual Basic complains that it cannot convert an Object into a String. But wait. You're trying to do the opposite: pass a String in as an Object variable. That should work because a String is a type of Object (it inherits from Object).

The trick is that this parameter is declared without the ByVal keyword. That means it is passed by reference and the Open routine might change the Object's value. It cannot do that if you pass it a String instead of an Object so it complains. A bit backwards but it makes some sense.

 
Private Sub btnOpen_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOpen.Click
    Dim word_app As Word.Application
    Dim word_doc As Word.Document

    ' Make a Word server object.
    word_app = New Word.Application
    word_app.Visible = False

    ' Open the Document.
    On Error GoTo OpenError
    word_doc = _
        word_app.Documents.Open(DirectCast(txtFile.Text, _
        Object))
    On Error GoTo 0

    ' Remove personal information.
    word_doc.RemovePersonalInformation = True

    ' Save and close the document.
    word_doc.Close(True)
    word_doc = Nothing

    ' Clean up.
    word_app.Quit()
    word_app = Nothing

    MessageBox.Show("Ok")
    Exit Sub

OpenError:
    MessageBox.Show("Error opening file." & vbCrLf & _
        Err.Description)
    word_app.Quit()
    word_app = Nothing
    Exit Sub
End Sub
 
Note that the RemovePersonalInformation property is relatively new so this will not work on older versions of Word. It works with the Word 10.0 object library but not with the 9.0 library.

For more information on programming Word and other Microsoft Office applications with VBA, see my book Microsoft Office Programming: A Guide for Experienced Developers.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated