I have a docx templates which has many shapes and each shape has a bookmark name.
Through a VBA code, I need to copy the shapes, while passing the bookmark name, and paste into destination new Word document.
I had an old template with .doc file format and below VBA code worked.
If SrcDoc.Bookmarks.Exists(bmName) Then
SrcDoc.Shapes(bmName).Select
WrdApp.Selection.Copy
DestDoc.Activate
DestDoc.Bookmarks("\EndOfDoc").Select
WrdApp.Selection.PasteAndFormat wdPasteDefault
I tried the below lines of code. And the error given for each line of code. Each line tried separately.
SrcDoc.Shapes.Range(Array(bmName)).Select '**-- The item with specified name wasn't found**
SrcDoc.Bookmarks("bmName").Select '**-- The requested member of the collection does not exist.**
I've tested your code for docx without issues on M365.
Please try to verify shapes' name, make sure the shape named bmName
is exists.
Sub ListShp()
Dim Shp As Shape
For Each Shp In ActiveDocument.Shapes
Debug.Print Shp.Name
Next
End Sub
Sub InserShpBookmark()
Dim shp As Shape
Set shp = ActiveDocument.Shapes.AddShape(msoShapeRectangle, _
Left:=100, _
Top:=100, _
Width:=200, _
Height:=100)
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="TestBK"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Debug.Print ActiveDocument.Bookmarks("TestBK").Start ' 0
Debug.Print ActiveDocument.Shapes.Count
Debug.Print ActiveDocument.Shapes(1).Name ' Rectangle 1
Debug.Print ActiveDocument.Shapes("TestBK").Name ' error
End Sub