View Single Post
 
Old 04-17-2019, 07:23 AM
one4youman one4youman is offline Windows 7 32bit Office 2010
Novice
 
Join Date: Apr 2019
Posts: 4
one4youman is on a distinguished road
Default

Any suggestions on how the code should look to pull the Heading styles?

Ideally, I would select the contract from a drop down combo box (Named cboContractForm) which corresponds to the bookmark that surrounds contract 1, contract 2, etc... click a button named cmdLoad, which then loops through the bookmarks contained / nested within the contract bookmark and loads the heading text from each bookmark to define the caption.

It would be similar to the following, but I am struggling to figure out how to setup the code to achieve this.



Code:
Private Sub UserForm_Initialize()
        With cboContractForm
                .AddItem "Contract 1"
                .AddItem "Contract 2"
        End With
End Sub


Based on the selected contract, update the check boxes for each term:

Code:
Private Sub cmdLoad_Click()
Dim Contract as String
If cboContractForm = "Contract 1" Then Contract = “Contract1” ‘Name of Bookmark Range
If cboContractForm = "Contract 2" Then Contract = “Contract2” ‘Name of Bookmark Range

    Dim bmk As Bookmark
    Dim i As Long
    Dim P As String
    Dim CovOutput As String
    Dim msg As String

        i = 1
        For Each bmk In ActiveDocument.Bookmarks(Contract).Range.Bookmarks 'Bookmark for entire contract - Set based on cboContractForm combobox.
            CovOutput = bmk.Name
            P = i

            Controls("CheckBox" & P).Tag = bmk.Name 'Set Tag equal to the bookmark that corresponds with the term being loaded into the Userform. If the text box is false I will use the tag name to determine which bookmarks to clear from the form.
            Controls("CheckBox" & P).Caption = Word 's Heading Styles for your Term text? 'Heading Text from within the found / looped bookmark.
            Controls("CheckBox" & P).ControlTipText = bmk 'Mouse over to show the entire term

            msg = msg & bmk.Name & vbCr

            i = (i + 1)
            On Error GoTo ErrorStop
        Next bmk

ErrorStop:    
    'Uncomment for Debug
    'MsgBox msg

End Sub


After selecting the applicable text boxes, click OK to update the letter.

Code:
Private Sub cmdOK_Click()
        On Error Resume Next
With ActiveDocument
If cboContractForm = "Contract 1" Then .Bookmarks("Contract1").Range.Text = ""
If cboContractForm = "Contract 2" Then .Bookmarks("Contract2").Range.Text = ""
Else
'Remove terms that do not apply
If CheckBox1 = False Then .Bookmarks("CheckBox1.Tag.Value").Range.Text = ""
If CheckBox2 = False Then .Bookmarks("CheckBox2.Tag.Value").Range.Text = ""
If CheckBox3 = False Then .Bookmarks("CheckBox3.Tag.Value").Range.Text = ""
If CheckBox4 = False Then .Bookmarks("CheckBox4.Tag.Value").Range.Text = ""
'etc for each check box
End If
End With
End Sub
 


I have never used header styles to pass values / extract text and I am not quite sure how to obtain values or reference ranges. The above codes do not work, but help to explain my thought process. Any updates would be greatly appreciated.

Thank you,
John




Reply With Quote