?
Solved

How do you programmatically delete ALL images from a word doc using VBA?

Posted on 2009-07-10
13
Medium Priority
?
817 Views
Last Modified: 2012-06-27
I would like to remove all images from a document programmatically.  I'm using Microsoft Office 2003 and the word 11.0 object library.
I've seen this question answered elsewhere with the following script :

    Dim objPic As InlineShape
    For Each objPic In word_doc.InlineShapes
        objPic.Delete
    Next objPic

Whilst there are circumstances where this code removes some of the images, there are plenty of occasions where it does not.  I have a document with a header/footer containing images, these were for example unaffacted.
Is there an enhancement to this code that would literally clean ALL images, wherever they may be.  I am not concerned with how the layout of the document might be adversely affected.
thanks!
0
Comment
Question by:dgloveruk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
13 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24825838
Try this macro from (I tested it and it worked for headers and footers too)
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
Sub FasterFindAndReplaceAllStoriesHopefully()
 
Dim myStoryRange As Range
 
'First search the main document using the Selection
 With Selection.Find
    .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
 
'Now search all other stories using Ranges
For Each myStoryRange In ActiveDocument.StoryRanges
   If myStoryRange.StoryType <> wdMainTextStory Then
        With myStoryRange.Find
            .Text = "^g"
            .Replacement.Text = ""
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
        Do While Not (myStoryRange.NextStoryRange Is Nothing)
           Set myStoryRange = myStoryRange.NextStoryRange
            With myStoryRange.Find
                .Text = "^g"
                .Replacement.Text = ""
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Loop
    End If
Next myStoryRange
End Sub

Open in new window

0
 

Author Comment

by:dgloveruk
ID: 24837963
I'm afraid this did not remove the image from the file.
I've attached a sample document so you can see the code not working / more easily find a working solution.
Ali-F-133101.doc
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24841500
Oh - I see - I believe it's not working properly because the image is set to "square" wrapping... let me see if I can figure something out.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24841582
I'm not having much luck.... hoping another expert can help you with this.
0
 
LVL 18

Expert Comment

by:JR2003
ID: 24897767
This works in Word 2007
Option Explicit
Sub DeletePictures()
    
    Dim myShape As InlineShape
    Dim myRange As Range
    For Each myRange In ActiveDocument.StoryRanges
        For Each myShape In myRange.InlineShapes
            myShape.Delete
        Next myShape
    Next myRange
 
End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:JR2003
ID: 24897888
I just tried it on you attached document and it didn't work, so I've modified it:
Option Explicit
Sub DeletePictures()
    
    Dim myInlineShape As InlineShape
    Dim myHeaderFooter As HeaderFooter
    Dim mySection As Section
    Dim myShape As Shape
    Dim myRange As Range
    
    For Each myRange In ActiveDocument.StoryRanges
        For Each myInlineShape In myRange.InlineShapes
            myInlineShape.Delete
        Next myInlineShape
    Next myRange
    
    For Each mySection In ActiveDocument.Sections
        For Each myHeaderFooter In mySection.Headers
            For Each myShape In myHeaderFooter.Shapes
                myShape.Delete
            Next myShape
        Next myHeaderFooter
        For Each myHeaderFooter In mySection.Headers
            For Each myShape In myHeaderFooter.Shapes
                myShape.Delete
            Next myShape
        Next myHeaderFooter
    Next mySection
End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
JR2003 earned 2000 total points
ID: 24897900
second myHeaderFooter loop for should have been footers:
Option Explicit
Sub DeletePictures()
    
    Dim myInlineShape As InlineShape
    Dim myHeaderFooter As HeaderFooter
    Dim mySection As Section
    Dim myShape As Shape
    Dim myRange As Range
    
    For Each myRange In ActiveDocument.StoryRanges
        For Each myInlineShape In myRange.InlineShapes
            myInlineShape.Delete
        Next myInlineShape
    Next myRange
    
    For Each mySection In ActiveDocument.Sections
        For Each myHeaderFooter In mySection.Headers
            For Each myShape In myHeaderFooter.Shapes
                myShape.Delete
            Next myShape
        Next myHeaderFooter
        For Each myHeaderFooter In mySection.Footers
            For Each myShape In myHeaderFooter.Shapes
                myShape.Delete
            Next myShape
        Next myHeaderFooter
    Next mySection
End Sub

Open in new window

0
 

Author Comment

by:dgloveruk
ID: 24902277
Thank you for your sugggestion,
The shape object doesn't seem to have a .delete method... is this code that would work in word 2003 with the document object model 11?
0
 
LVL 18

Expert Comment

by:JR2003
ID: 24902454
I've just tried it as a macro from Word 2003 (11.8202.8202) SP3 and it worked.
How are you running it?
0
 

Author Closing Comment

by:dgloveruk
ID: 31602166
Your follow up question abtou what I was using led me to the cause of the last hickup.  I was using VB6 and the object types you had declared were object types used by other references and I needed to dimension the variables as word.shape not just shape to get the methods needed.
Thank you for your help, I've been stuck on this one a while!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question