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 pictures from a Word document
KeywordsWord, picture, shape, InlineShape, remove, graphic
CategoriesOffice, Utilities
 
Note that this program requires a reference to the Microsoft Word Object Library (In the Project menu, select References and check the box next to the library).

This example demonstrates two methods. First, it creates a Word server object and opens the source file. It uses the Selection.Find object to find and replace all graphic objects with nothing. This removes InlineShapes but not Shapes.

When it is finished, the program saves the Word document with a new name and closes the Word server.

 
' Remove the pictures by replacing graphic objects.
Private Sub cmdReplacePictures_Click()
Dim word_app As Word.Application

    Set word_app = New Word.Application
    word_app.ChangeFileOpenDirectory App.Path
    word_app.Documents.Open _
        FileName:=txtSource.Text, _
        ConfirmConversions:=False, _
        ReadOnly:=True, _
        AddToRecentFiles:=False, _
        Format:=wdOpenFormatAuto

    With word_app.Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^g"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With

    ' Save the file.
    word_app.ActiveDocument.SaveAs _
        FileName:=txtResult.Text, _
        AddToRecentFiles:=False

    word_app.Quit
    Set word_app = Nothing

    MsgBox "Okay"
End Sub
 
In the second method, the program creates a Word server object and opens the source file as before. It then loops through the document's InlineShapes and Shapes collections, deleting all of their entries. Note that this removes objects other than pictures (for example, OLE objects). You could make the code test the type of object to see what it is deleting if you want.

When it is finished, the program again saves the Word document with a new name and closes the Word server.

 
' Remove Shapes and InlineShapes.
Private Sub cmdRemoveShapes_Click()
Dim word_app As Word.Application
Dim word_doc As Word.Document

    Set word_app = New Word.Application
    word_app.ChangeFileOpenDirectory App.Path
    Set word_doc = word_app.Documents.Open( _
        FileName:=txtSource.Text, _
        ConfirmConversions:=False, _
        ReadOnly:=True, _
        AddToRecentFiles:=False, _
        Format:=wdOpenFormatAuto)

    ' Delete InlineShapes.
    With word_doc.InlineShapes
        Do While .Count > 0
            .Item(1).Delete
        Loop
    End With

    ' Delete Shapes.
    With word_doc.Shapes
        Do While .Count > 0
            .Item(1).Delete
        Loop
    End With

    ' Save the file.
    word_app.ActiveDocument.SaveAs _
        FileName:=txtResult.Text, _
        AddToRecentFiles:=False

    word_app.Quit
    Set word_app = Nothing

    MsgBox "Okay"
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated