I have a long MS Word 2007 document, in which I occasionally need to replace all of the embedded graphs.

The changes are simple enough and regular enough that I could probably write a VBA macro to make these updates for me.

But, I've come into a very basic problem: I can't figure out how to even select an embedded graph in VBA. The "Record Macro" feature in Word doesn't record changes to graph for some reason, and all of the online tutorials address interacting with graphs in Excel.

Could someone please post some short example code (or a link explaining) that shows some basic interaction with embedded graphs in Word?


Have a look at this post in the MSDN Blog: Office Chart Object Model in PowerPoint and Word

It requires SP2 for Office 2007.

  • Perfect. That was exactly what I needed -- thanks! – anschauung Nov 20 '09 at 15:59

This should work for both embedded images (jpg, etc) or embedded charts:

'Kill 'em all: pictures, OLE object, hyperlinks, ActiveX controls, etc.
Sub DeleteAllShapes()
    Dim shp As InlineShape

    For Each shp In ActiveDocument.InlineShapes
    Next shp
End Sub

There is also a "Shape" object (and Shapes collection) that refers to all shapes more generically - whether they are inline or not. A shape can be many things - use the Type property to return the type of inline shape: picture, linked picture, embedded OLE object, linked OLE object, ActiveX controls and more. If you have many different types and need to distinguish them, you can surround your delete with something like:

If shp.Type = wdInlineShapeEmbeddedOLEObject Then
End If

One way to add a new picture:

' Insert a picture at the current insertion point.
Sub InsertPicture(ByVal FileName As String)

    Selection.InlineShapes.AddPicture _
                             FileName:=FileName, _
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.