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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Maybe just run this code to convert the shapes to inlineshapes and use your original code. HTH. Dave
Code:
 For Each oShp In ActiveDocument.Shapes       
 oShp.ConvertToInlineShape   
 Next oShp
 
Upvote 0
Thanks for the suggestion... I am getting a Bad Parameter error on the oShp.ConvertToInlineShape line. Any ideas?
 
Upvote 0
Maybe declare the variable as object (or Shape) and change the active document to your oDoc. Dave
Code:
Dim oShp as object ' Shape
For Each oShp In oDoc .Shapes       
 oShp.ConvertToInlineShape   
 Next oShp
 
Upvote 0
I had oShp declared as an object and I did change to used oDoc. That results in a bad parameter error. I tried declaring as a Shape and a ShapeRange per your suggestion. Both result in a type mismatch error on the For each oShp line. This is my first attempt at working on a Word Doc from excel. Are shapes the same across both Applications?
 
Upvote 0
Maybe you're trying to convert an inline shape? Don't really know. U can trial this. Dave
Code:
Dim oShp as object ' Shape
For Each oShp In oDoc .Shapes    
If .Type <> 3 then 'wdInlineShapePicture
oShp.ConvertToInlineShape 
End if
Next oShp
U can look at this as well. It involves using selection and refers to shaperanges...
 
Upvote 0
Whoops.... the previous code won't help if you're code is crashing on the For each line. I'll Google some more. Dave
 
Upvote 0
I've used this before without problems...
Code:
Dim ObjPic As Object
For Each ObjPic In PFWdApp.ActiveDocument.InlineShapes
ObjPic.ConvertToShape
Next ObjPic
where the PFWdApp is the name of the Word application so....
Code:
Dim ObjPic As Object
For Each ObjPic In PFWdApp.ActiveDocument.Shapes
ObjPic.ConvertToInlineShape
Next ObjPic
seems reasonable. Maybe you're just missing the Word application?
Dave
 
Upvote 0
So I learned a few things after fiddling with the shapes a bit more. The shapes were actually grouped (shape.type = 6). The word documents were created by coverting a PDF to a word doc. The shape type of the picture (which actually should be bookmarked and hyperlinked when ungrouped is shape.type 13 mso picture. After ungrouping the shapes, I was able to covert to inline shapes using the code above as suggested. However this resizes the pictures to a little sliver and makes it unusable. Any suggestions?
 
Upvote 0
So I got something to work. Since I was having so much trouble with working from excel in word, I just moved everything over to word. There was a few changes I had to make, but all in all it went smoothly. Up until now, I had only used VBA in excel so that's where I started. Thanks for the help Dave!
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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