PDA

View Full Version : [SOLVED:] Problem producing text depending on choices made from a form



VeryTerry
02-14-2014, 11:33 AM
I am stuck with trying to get specific text to print out depending on a user’s choice on a User Form. I’m not a skilled vba-er; I’ve been trying to adapt existing code to my purpose, and have tried many different approaches. I am using Word 2010, VBA 7.0.

Here’s the scenario: I am adding “letterhead” graphics and text to the top of a letter. The address vba code works fine. But I want to add text to the bottom based on what choice the user makes in a form:



frmSimulateHCSLetterhead.Show

Here’s the code in the form:


Private Sub CmdOK_Click()

Dim p_sContractorLic As String

Select Case Me.OptAZ.Value
Case True: p_sContractorLic = "AZ"
Unload Me
Case False:
End Select

Select Case Me.OptFL.Value
Case True: p_sContractorLic = "FL"
Unload Me
Case False:
End Select

Select Case Me.OptCA.Value
Case True: p_sContractorLic = "CA"
Unload Me
Case False:
End Select

Select Case Me.OptNone.Value
Case True: p_sContractorLic = ""
Unload Me
Case False:
End Select

End Sub

And here is the related code in the document. I want to find the text “CLN”, delete it (find a bookmark) and insert the number associated with the user’s choice in the form:


Dim strContractorLic As String

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Collapse Direction:=wdCollapseStart
Set rTemp = Selection.Range
rTemp.Find.Execute findtext:="CLN", Forward:=True
rTemp.Select

If p_sContractorLic = "AZ" Then
strContractorLic = "280646"
End If
If p_sContractorLic = "CA" Then
strContractor = "848414"
End If
If p_sContractorLic = "FL" Then
strContractor = "CGC1520478"
End If
If p_sContractorLic = "" Then
strContractor = ""
End If

With Selection
.Delete
.InsertAfter Text:=p_sContractorLic
.Collapse Direction:=wdCollapseEnd
End With


I also tried using Select Case
Can anyone help?

westconn1
02-14-2014, 02:59 PM
the select case should look like

Private Sub CmdOK_Click()

Dim p_sContractorLic As String

Select Case true ' only one optionbutton in a group can be true
case Me.OptAZ.Value
p_sContractorLic = "AZ"
Case Me.OptFL.Value
p_sContractorLic = "FL"
Case Me.OptCA.Value
p_sContractorLic = "CA"
Case Me.OptNone.Value
p_sContractorLic = ""
End Select
' now do something with p_sContractorLic as it is a variable defined locally, it will be out of scope at the end of the procedure

unload me
End Sub if you dimension p_sContractorLic as a global (or module level) variable,
that is in the general section at the top of a standard module, your code may then work,
i do not see any reason to do the same job twice though,
either assign the subcontractors licence number in the form code, or find the selected option button in the document code,
instead of me.unload, use me.hide, use the form name instead of the me keyword

VeryTerry
02-19-2014, 11:39 AM
Dear Westconn1 - thank you for your speedy reply. I've tried the code you gave me, but I still can't get the text to print out. The problem might originate from another part of the macro (it's a long complex one). I've contracted with a VBAExpress consultant to help with a number of macros now, so hopefully I'll get it resolved that way. I'll let you know what I find out. Thank you again...

westconn1
02-20-2014, 03:49 AM
I've tried the code you gave me, but I still can't get the text to print out.i did not give you a solution in code, just an example on Select Case, the information at the bottom should possibly have been a help towards a solution, if you were able to understand