I need to generate bookmarks in Word 2010 programmatically, with the header name as the bookmark name.
I have the following code which makes a word a bookmark, but the bookmark name remains the same as the string Heading 1
is only available in the name variable:
Sub bookmarking()
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=" Heading 1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub
Instead of the Heading 1
in the name variable, I want content from the clipboard. Please help me replace that Heading 1
with clipboard content.
Use a
DataObject
from theMicrosoft Forms 2.0 Object Library
:Then, back your main code, have the bookmark name be this clipboard data:
Is this a good start for you? There are other ways which may be more robust depending on your needs. However this should serve as good proof-of-concept.