Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

It requires SP2 for Office 2007.

share|improve this answer
    
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
        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
share|improve this answer
    
Very useful -- thanks! –  anschauung Nov 20 '09 at 16:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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