Search code examples
ms-wordms-officeuserform

Userform for Word with bookmarks and automated Crossreferences


so this is my first programming experience and I am pretty excited about it. However, I have been dealing with a problem:

Goal

My goal is to create a word document with an integrated Userform asking for Name, Title and Startdate at the beginning. The information shall then be reflected in the defined areas in the document.

Dim Name As Range
Set Name = ActiveDocument.Bookmarks("Name").Range
Name.Text = Me.TextBox1.Value
Dim Title As Range
Set Title = ActiveDocument.Bookmarks("Title").Range
Title.Text = Me.TextBox2.Value
Dim Startdate As Range
Set Startdate = ActiveDocument.Bookmarks("Startdate").Range
Startdate.Text = Me.TextBox3.Value
Me.Repaint
UserForm1.Hide

I could successfully integrate the Userform, assigning the information to the bookmarks. Since I can only use 1 bookmark I tried my luck with text properties but it does not work.

The next thing I tried was to apply 1 bookmark and several cross-references throughout the text, meaning that I have to add a command to the code that updates all cross-references automatically I tried it with sub updateAllFields()

Dim Name As Range
    Set Name = ActiveDocument.Bookmarks("Name").Range
    Name.Text = Me.TextBox1.Value
    Dim Title As Range
    Set Title = ActiveDocument.Bookmarks("Title").Range
    Title.Text = Me.TextBox2.Value
    Dim Startdate As Range
    Set Startdate = ActiveDocument.Bookmarks("Startdate").Range
    Startdate.Text = Me.TextBox3.Value
    Me.Repaint
    Sub UpdateAllFields()
    UserForm1.Hide

But this one gives me an error message. Can somebody help, please?


Solution

  • Stick to the fields path as it may be less work to maintain.


    I: Create three custom document properties for each field (Name, Title, StartDate)

    Steps:
    1) Click File | Properties | Advanced properties | Custom
    2) Enter the following data:
    - Name: customName
    - Text: Enter name
    3) Click the button "Add"
    4) Repeat steps 2 and 3 for fields Title and StartDate (remember to add the "custom" word before so you can identify them later easily

    This is how it should look like:
    enter image description here


    II: Add the fields of each custom property created to the word document

    Steps:
    1) Position the cursor where you want the field in the word document
    2) Click Insert | Quick parts | Field | Categories: Document information | DocProperty | < name of the doc property >
    3) Click Ok
    4) Repeat the process for each field

    enter image description here

    Note: You can add the same field / custom property, in different parts of the document

    enter image description here


    III: Create the UserForm and add the controls

    Steps:
    1) Add the UserForm
    2) Add three textboxes and set the name property to:
    - txtName
    - txtTitle
    - txtStartDate
    for each one.
    3) Add a command button and set its name to cmdInsertData

    This is how it should look like:

    enter image description here

    To set the name of each control look for (Name) at the properties window:

    enter image description here


    IV: Add code to show the userform at startup

    Steps:
    1) Launch the VBE by pressing Alt + F11 (or activating the developer tab in the ribbon and pressing the Visual basic button
    2) Add the following code to the "ThisDocument" object:

    Private Sub Document_Open()
    
        UserForm1.Show
    
    End Sub
    

    V: Add the form code

    Steps:
    1) Right click the UserForm1 object and select "View Code" 2) Add the following code:

    Private Sub cmdInsertData_Click()
    
        ' Update the properties values
        ThisDocument.CustomDocumentProperties("customName").Value = Me.txtName.Value
        ThisDocument.CustomDocumentProperties("customTitle").Value = Me.txtTitle.Value
        ThisDocument.CustomDocumentProperties("customStartDate").Value = Me.txtStartDate.Value
    
        ' Show changes of document properties in document
        ThisDocument.Fields.Update
    
        ' Hide the userform
        UserForm1.Hide
    
    End Sub
    

    Remember to save your document as macro-enabled

    Close it and reopen it

    Give it a try and let me know

    Disclaimer: As this article states, there is a right way to handle showing a userform properly, but I'll leave it the "easy" way for simplicity purposes.