Easy One? - Input Box with Two Fields in Word Macro?

In Word, is there a macro code or a way to create an Input Box with two fields for two instances of input data like you can in Filemaker, for example?
LVL 2
rvfowler2Asked:
Who is Participating?
 
PandaPantsCommented:
Not really, as far as I know, but you can ask the user to put in a delimiter of some sort. For example:


Public Sub InputBox2Params()
    Dim sInputVal As String
    Dim sValue1 As String, sValue2 As String
    Dim iOctothorpPosition As Integer
    sInputVal = InputBox(Prompt:="Please type in two values, one in front of the octothorp (#) and the other after it.", _
                                    Title:="Type in two values", Default:=" # ")
    iOctothorpPosition = InStr(1, sInputVal, "#", vbTextCompare)
    sValue1 = Trim(Mid(sInputVal, 1, iOctothorpPosition - 1))
    sValue2 = Trim(Mid(sInputVal, iOctothorpPosition + 1))

    MsgBox "sValue1 = " & sValue1 & vbCrLf & "sValue2 = " & sValue2

End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
Other alternatives are to use two succesive InputBox statements, or to employ a UserForn with two textboxes.
0
 
PandaPantsCommented:
FWIW, in practice, I usually go with the UserForm idea that GrahamSkan mentions.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
rvfowler2Author Commented:
Thanks, I'll look into UserForms.  Been a avery long time since I used or designed one.
0
 
PandaPantsCommented:
For this example, create a userform and add two textboxes (named txtThingOne and txtThingTwo) and two buttons (cmdCancel and cmdOK), and put the following code behind those buttons.

Note that the code in cmdOK_Click() refers to the CDP creation routines we discussed earlier; I've included those here, too, just for completeness' sake. Hope this helps.
Private Sub cmdCancel_Click()
    MsgBox prompt:="Note: No values have been saved for Thing One or Thing Two.", Buttons:=vbOKCancel & vbCritical, Title:="Missing Data"
    Unload Me
End Sub

Private Sub cmdOK_Click()
    Dim bGotItAll As Boolean
    Dim sThingOne As String, sThingTwo As String
    sThingOne = Trim(txtThingOne.Value)
    sThingTwo = Trim(txtThingTwo.Value)
    bGotItAll = False
    If sThingOne = "" Then
        MsgBox prompt:="Please enter something into Thing One.", Buttons:=vbOKCancel & vbCritical, Title:="Missing Data"
    ElseIf sThingTwo = "" Then
        MsgBox prompt:="Please enter something into Thing Two.", Buttons:=vbOKCancel & vbCritical, Title:="Missing Data"
    Else
        bGotItAll = True
    End If
    If bGotItAll Then
        CreateAndPopulateOneCDP "cdpThingOne", sThingOne
        CreateAndPopulateOneCDP "cdpThingTwo", sThingTwo
        Unload Me
    End If ' bGotItAll
End Sub

'==================
'--------------
Public Sub CreateAndPopulateOneCDP(ByVal psCdpName As String, ByVal psCdpValue As String)
        If CdpExists(psCdpName) Then
            ActiveDocument.CustomDocumentProperties(psCdpName) = psCdpValue
        Else ' CdpExists(psCdpName) = False
            ActiveDocument.CustomDocumentProperties.Add _
                Name:=psCdpName, LinkToContent:=False, Value:=psCdpValue, _
                Type:=msoPropertyTypeString
        End If ' CdpExists(psCdpName)
End Sub ' CreateAndPopulateOneCDP(ByVal psCdpName As String, ByVal psCdpValue As String)
'--------------
Public Function CdpExists(ByVal CdpName As String) As Boolean
Dim xx As DocumentProperty
    For Each xx In ActiveDocument.CustomDocumentProperties
            If LCase(xx.Name) = LCase(CdpName) Then
                CdpExists = True
                Exit Function
            End If ' xx.Name <> CdpName
    Next xx
    CdpExists = False
End Function
'--------------

Open in new window

0
 
rvfowler2Author Commented:
Thanks, it does help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.