Search code examples
vbams-word

How to access manually created SmartArt objects through VBA in Microsoft Word


SmartArt objects that are manually created (i.e. through the GUI) in Microsoft Word do not show up in the ActiveDocument.Shapes collection. I.e. if I start with a blank document, and then manually create a SmartArt with some content in it, and then call ActiveDocument.Shapes.Count - it returns 0.

However, if I create the SmartArt using VBA as such, then it shows up in the ActiveDocument.Shapes collection.

Dim myShape As shape
Dim mySmartArt As SmartArt

Set myShape = ActiveDocument.Shapes.AddSmartArt(Application.SmartArtLayouts(1), 100, 100, 400, 400)
    Set mySmartArt = myShape.SmartArt

So far I have not been able to find any resources that point me in the direction of accessing SmartArt shapes in Word through VBA.

I have tried using the 'Record Macro' function to generate the VBA it runs when I create the SmartArt manually, however, this simply returns a blank macro.

Any insights would be appreiciated!


Solution

  • When inserted via the ribbon you can access the smart art through InlineShapes collection

    You can "migrate" them to "normal" shapes like this and then access them as you expect it.

    Sub convertInlineSmartArtToShape()
    
    Dim iShp As InlineShape
    For Each iShp In ThisDocument.InlineShapes
        If iShp.HasSmartArt Then
            iShp.ConvertToShape
        End If
    Next
    
    End Sub