David Glover
asked on
How do you programmatically delete ALL images from a word doc using VBA?
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!
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!
ASKER
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
I've attached a sample document so you can see the code not working / more easily find a working solution.
Ali-F-133101.doc
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.
I'm not having much luck.... hoping another expert can help you with this.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
I've just tried it as a macro from Word 2003 (11.8202.8202) SP3 and it worked.
How are you running it?
How are you running it?
ASKER
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!
Thank you for your help, I've been stuck on this one a while!
http://www.word.mvps.org/F
Open in new window