PDA

View Full Version : Dropdown Lists & Calculations



jsyshaggy
02-19-2014, 05:32 AM
Hi,
I've been searching for days and am only able to come up with a method using legacy dropdowns, which is quite restrictive and doesn't always update.

I need 2 dropdown lists and a text field in a word document. The first dropdown will contain 3 choices - 1) Please choose 2) Likely & 3) Unlikely with respective values 0, 1 & 2. The second list needs to contain 6 choices "Please choose" & Low to High with the values 0 to 5 again. Finally the Text field should be updated with a value which is the result of multiplying the dropdown list values together.
e.g.
Dropdown1 = 2
Dropdown2 = 4
Text1 = 8

I have found lots of resources for creating macros for ContentControl's but can't find anything specific

Thanks

gmaxey
02-19-2014, 08:15 PM
It isn't easy, but provided the CCs are titled CC1 and CC2 then something like this;


Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim lngVal1 As Long, lngVal2 As Long
Dim lngIndex As Long
Dim oCC As ContentControl
Select Case CC.Title
Case "CC1", "CC2"
Select Case CC.Title
Case "CC1"
For lngIndex = 1 To CC.DropdownListEntries.Count
If CC.Range.Text = CC.DropdownListEntries(lngIndex).Value Then
lngVal1 = lngIndex - 2
Exit For
End If
Next
Set oCC = ActiveDocument.SelectContentControlsByTitle("CC2").Item(1)
For lngIndex = 1 To oCC.DropdownListEntries.Count
If oCC.Range.Text = oCC.DropdownListEntries(lngIndex).Value Then
lngVal2 = lngIndex - 2
Exit For
End If
Next
Case "CC2"
For lngIndex = 1 To CC.DropdownListEntries.Count
If CC.Range.Text = CC.DropdownListEntries(lngIndex).Value Then
lngVal2 = lngIndex - 2
Exit For
End If
Next
Set oCC = ActiveDocument.SelectContentControlsByTitle("CC1").Item(1)
For lngIndex = 1 To oCC.DropdownListEntries.Count
If oCC.Range.Text = oCC.DropdownListEntries(lngIndex).Value Then
lngVal1 = lngIndex - 2
Exit For
End If
Next
End Select
MsgBox lngVal1 * lngVal2
End Select

End Sub

jsyshaggy
02-20-2014, 09:15 AM
Thanks. One of the guys who used to deal with all our excel macros is back in today so I have passed it onto him as I couldn't get it to work. I doubt this is your VB rather my skills in getting it into the document

gmaxey
02-20-2014, 01:57 PM
Well, I should have told you that the code goes in the ThisDocument module of the VB Project.

jsyshaggy
02-21-2014, 02:04 AM
Well thats what I thought I had done anyway :dunno

jsyshaggy
02-21-2014, 02:11 AM
And again with the right file