PDA

View Full Version : [SOLVED:] Userform Combobox in Word



klg
12-12-2018, 02:50 PM
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

gmaxey
12-13-2018, 03:17 PM
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

klg
12-14-2018, 11:40 AM
Beauty!!! Thank you so much!

gmaxey
12-14-2018, 11:47 AM
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.

klg
12-14-2018, 02:25 PM
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"

gmaxey
12-14-2018, 03:13 PM
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.

klg
12-17-2018, 12:56 PM
Wow, I sincerely appreciate this!

Once again, Thank-you!