Working with shapes in word from excel VBA

atr140

Board Regular
Joined
Nov 27, 2012
Messages
72
All,

I am trying to loop through a word document and add bookmarks to all shapes (which are inserted pictures). I have successfully done this when the pictures were inline shapes.

Successful code when inline shapes
VBA Code:
Private Sub LoopThroughDocument_Bookmark()

    'Define sub/func for error handler
    Dim sSubFunc As String: sSubFunc = "LoopThroughDocument_Bookmark"

    On Error GoTo eh

1:    Dim i As Long
2: With oDoc
3: For i = 1 To .inlineshapes.Count: Debug.Print .inlineshapes.Count
4: If .inlineshapes.Item(i).Type = 3 Then 'wdInlineShapePicture enumeration = 3

'Add Bookmarks to photos - hyperlinks will be made in later step
5: .Bookmarks.Add Name:="P" & i, Range:=.inlineshapes(i).Range: 'Debug.Print i

'Add Hyperlink to Cover page - bookmark will be made in later steps
6: .Hyperlinks.Add Anchor:=.inlineshapes(i).Range, Address:="", _
SubAddress:="TB" & i, ScreenTip:="", TextToDisplay:=v
7: End If
8: Next i
9:    End With


Done:
Exit Sub
eh:
   RaiseError Err.Number, Err.Source, sModule & "." & sSubFunc, Err.Description, Erl

End Sub

As stated previously, some word docs have the pictures inserted as shapes. I assumed this was a simple fix like:

VBA Code:
Private Sub LoopThroughDocument_Bookmark2()

    'Define sub/func for error handler
    Dim sSubFunc As String: sSubFunc = "LoopThroughDocument_Bookmark2"

    On Error GoTo eh

1:    Dim i As Long
2: With oDoc
3: For i = 1 To .Shapes.Count:

'Add Bookmarks to photos - hyperlinks will be made in later step
5: .Bookmarks.Add Name:="P" & i, Range:=.Shapes(i).Range: 'Debug.Print i

'Add Hyperlink to Cover page - bookmark will be made in later steps
6: .Hyperlinks.Add Anchor:=.Shapes(i).Range, Address:="", _
SubAddress:="TB" & i, ScreenTip:="", TextToDisplay:=v
8: Next i
9:    End With

Done:
Exit Sub
eh:
   RaiseError Err.Number, Err.Source, sModule & "." & sSubFunc, Err.Description, Erl

End Sub

I have tried many different approaches however I keep getting an error on line 5 (Object doesn't support this property). I used the macro recorder within word to try to figure out the syntax. The macro recorder does produce results, but the shape must be selected, and then the Range property of the boomark add is Selection.Range. I was trying to stay away from a select solution, but I decided to give it a try anyways. This resulted in the same error.

VBA Code:
Private Sub LoopThroughDocument_Bookmark2()

    'Define sub/func for error handler
    Dim sSubFunc As String: sSubFunc = "LoopThroughDocument_Bookmark2"

    On Error GoTo eh

1:    Dim i As Long
2: With oDoc
3: For i = 1 To .Shapes.Count:

.Shapes(i).select

'Add Bookmarks to photos - hyperlinks will be made in later step
5: .Bookmarks.Add Name:="P" & i, Range:=Selection.Range: 'Debug.Print i

'Add Hyperlink to Cover page - bookmark will be made in later steps
6: .Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="TB" & i, ScreenTip:="", TextToDisplay:=v
8: Next i
9:    End With

Done:
Exit Sub
eh:
   RaiseError Err.Number, Err.Source, sModule & "." & sSubFunc, Err.Description, Erl

End Sub

Thanks in advance for the help
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top