1

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?

2 Answers 2

1

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

It requires SP2 for Office 2007.

1
  • Perfect. That was exactly what I needed -- thanks!
    – anschauung
    Commented Nov 20, 2009 at 15:59
2

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
        shp.Delete
    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
    shp.Delete
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, _
                             SaveWithDocument:=True
End Sub
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .