1
votes

I'm creating an MS Word document with a table at the top with cells for 'Name', 'Date', 'Skills Verified' etc. and 10 questions in separate sections below this.

I'm creating a UserForm to make it simpler for all participants to complete the assessment on their PC's.

I'm using a MultiPage to show each question on a separate page of the UserForm and to display the question I'm attempting to populate the Caption of a Label by referencing the question on the document using an enclosed bookmark.

Please note: Other staff may edit or manage this document in future and most of them are not experienced at all with VBA or UserForms which is the main focus for what I'm attempting (plus it's efficient right?).

I've read up on bookmarks (wordmvp) and I understand how to set the bookmarks fine.

I can't work out how to get the contents of an enclosed bookmark to populate the Label Caption on UserForm initialization.

I've looked at the following info but haven't been able to make any sense of it. https://msdn.microsoft.com/en-us/vba/word-vba/articles/bookmarks-object-word https://msdn.microsoft.com/en-us/vba/word-vba/articles/captionlabels-object-word https://forum.solidworks.com/thread/209704

Assuming default names are used in the UserForm and the question "Is this a test question?" is in an enclosed bookmark named "bookmark1" in my document; below is the closest working code I could come up with.

Private Sub UserForm_Initialize()

Dim question1 As String
question1 = ActiveDocument.Bookmarks("bookmark1")

    With Selection
    Label1.Caption = question1
    End With

End Sub

However this displays the string "bookmark1" in the label1 caption, not "Is this a test question?".

I can't work out how to edit the code to make it show the text from the bookmark rather than the string "bookmark1" in the label1 caption (I'm not even sure if it's possible?).

Can anyone share where I've gone wrong and explain how to amend this?

1
Label1.Caption = Selection instead of Label1.Caption = question1?Vityata
What about question1 = ActiveDocument.Bookmarks("bookmark1").Range.Text ?PeterT
Thanks @PeterT, this has worked for what I need!Samuel Everson

1 Answers

2
votes

The bookmark can be declared as an Object. Thus, it has plenty of properties that can be accessed:

enter image description here

You can access almost anything from the b.Application or b.Range or b.Parent.

Private Sub UserForm_Initialize()

    Dim b As Bookmark
    Set b = ActiveDocument.Bookmarks("bookmark1")
    Label1.Caption = b.Range.Text

End Sub

Further, depending on what you are doing exactly, you can have more than 1 bookmark in a selection. Thus, you can access the Bookmarks collection from the selection and concatenate their text to a String

Private Sub UserForm_Initialize()

    Dim bookmarksInfo   As String
    Dim b               As Bookmark

    For Each b In Selection.Bookmarks
        bookmarksInfo = bookmarksInfo & b.Range.Text & vbCrLf
    Next b

    Label1.Caption = bookmarksInfo

End Sub