Consulting

Results 1 to 7 of 7

Thread: Userform Combobox in Word

  1. #1
    VBAX Regular
    Joined
    Jul 2018
    Posts
    9
    Location

    Userform Combobox in Word

    Hi there,

    I am trying to get a combox to list items in a userform in Word. It seems completed different then from Excel! I am missing something obvious....

    I can get the textboxes to flow through to the document (using document bookmarks), but I cannot get the combobox drop down items to show.
    The combobox is called Circumstances



    Private Sub Circumstances_Change()


    With Me.Circumstances
    .AddItem "we reviewed your plan"
    .AddItem "we discussed a tax strategy"
    .AddItem "you mentioned"
    .AddItem "recently purchased a home for $"
    .AddItem "You mentioned concerns about meeting your financial obligations if you were not longer able to work"
    .AddItem "are expecting a new addition to your family"
    .AddItem "you are the sole income earner"
    .AddItem "you have been requested by your lender to obtain life insurance to cover your business loan"
    .AddItem "you had concerns about your business continuing to operate should you become unable to work"
    .AddItem "we review your plan, which identified a need"
    End With




    End Sub
    Attached Files Attached Files

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You are using the wrong event. Since the Circumstance_Event is never changed, the code in it never runs. One way is to use the forms initialized event:

    Private Sub UserForm_Initialize()
      With Circumstances
        .AddItem "we reviewed your plan"
        .AddItem "we discussed a tax strategy"
        .AddItem "you mentioned"
        .AddItem "recently purchased a home for $"
        .AddItem "You mentioned concerns about meetinh your financial obligations if you were not longer able to work"
        .AddItem "are expecting a new addition to your family"
        .AddItem "you are the sole income earner"
        .AddItem "you have been requested by your lender to obtain life insurance to cover your business loan"
        .AddItem "you had converns about your business continuing to operate should you become unable to work"
        .AddItem "we review your plan, which identified a need"
        'Or
        .Clear
        Dim arrItems() As String
        arrItems = Split("A,B,C,D", ",")
        .List = arrItems
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Jul 2018
    Posts
    9
    Location
    Beauty!!! Thank you so much!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    BTW, since you are using a docm format file, you must have Word 2007 or higher. If your intended users will have Word 2007 or higher then writing your form results to a bookmark is a bit dated. Instead you might consider replacing the bookmarks with titled content controls and:

    ActiveDocument.SelectContentControlsByTitle("Circumstances").Item(1).Range. Text = Circumstances.Value

    You certainly do not need all of those declarations.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Jul 2018
    Posts
    9
    Location
    Well that is fantastic!

    Last question, how would I get the option button to work:

    ActiveDocument.SelectContentControlsByTitle ("Delivery")
    If OptionButton1.Value = True Then Delivery.Text = "inform you when to expect your policy in the mail"
    If OptionButton2.Value = True Then Delivery.Text = "arrange for its delivery"

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Well lets just clean the whole mess up while we are at it:

    In the form module use:

    Option Explicit
    
    Private Sub UserForm_Initialize()
      With Circumstances
        .AddItem "we reviewed your plan"
        .AddItem "we discussed a tax strategy"
        .AddItem "you mentioned"
        .AddItem "recently purchased a home for $"
        .AddItem "You mentioned concerns about meetinh your financial obligations if you were not longer able to work"
        .AddItem "are expecting a new addition to your family"
        .AddItem "you are the sole income earner"
        .AddItem "you have been requested by your lender to obtain life insurance to cover your business loan"
        .AddItem "you had converns about your business continuing to operate should you become unable to work"
        .AddItem "we review your plan, which identified a need"
      End With
      'Continue as above to fill other comboboxes.
    lbl_Exit:
      Exit Sub
    
    End Sub
    
    Private Sub Commandbutton1_Click()
      Tag = "CREATE DOC"
      Hide
    lbl_Exit:
      Exit Sub
    End Sub
    
    Private Sub cmdCanx_Click()
      Hide
    lbl_Exit:
      Exit Sub
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      If CloseMode = 0 Then
        Cancel = True
        cmdCanx_Click
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    In a new standard module use:

    Option Explicit
    Dim oFrm As Why 'Strange name and caption for a form.
    Sub AutoNew()
      Set oFrm = New Why
      With oFrm
        .Show
        If .Tag = "CREATE DOC" Then
          FillDoc
        Else
          ActiveDocument.Close wdDoNotSaveChanges
        End If
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    
    Sub FillDoc()
    Dim oCtrl As Object
      For Each oCtrl In oFrm.Controls
        Select Case TypeName(oCtrl)
          Case "TextBox"
            ActiveDocument.SelectContentControlsByTitle(oCtrl.Name).Item(1).Range.Text = oCtrl.Text
          Case "ComboBox"
            ActiveDocument.SelectContentControlsByTitle(oCtrl.Name).Item(1).Range.Text = oCtrl.Value
          Case "OptionButton"
            If oCtrl.Name = "Advisory" And oCtrl = True Then
              ActiveDocument.SelectContentControlsByTitle("Delivery").Item(1).Range.Text = "inform you when to expect your policy in the mail"
            ElseIf oCtrl.Name = "Mail" And oCtrl = True Then
              ActiveDocument.SelectContentControlsByTitle("Delivery").Item(1).Range.Text = "arrange for its delivery"
            End If
        End Select
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    
    'Left over code.
    Sub CleanUpBookmarkMess()
      Dim oBM As Bookmark
      Dim oCC As ContentControl
      For Each oBM In ActiveDocument.Bookmarks
        Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oBM.Range)
        oCC.Title = oBM.Name
        oCC.SetPlaceholderText , , oBM.Name
        oBM.Delete
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Rename your option buttons "Advisory" and "Mail"

    One of those attachments has a Stop in the code, but I couldn't figure out how to delete the attachment.
    Attached Files Attached Files
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Jul 2018
    Posts
    9
    Location
    Wow, I sincerely appreciate this!

    Once again, Thank-you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •