The aim of this procedure is to create a visual indication, by means of a bar chart, the value of a dropdown form field as in the animated illustration below. The animation will run indefinitely: | |
| |
| Each of the dropdown fields in column 1 of the table is configured similarly (create one and copy/paste the remainder). The drop down entries are numbers 0 to 10, and the macro ColTable1Row1 is run on exit from each. The illustration shows the default field bookmark name of Dropdown1. The bookmark names of the fields are immaterial as long as the final character is a number from 1 to 4 to reflect the row of the table. This number is used by the macro to fill the correct row of the table. |
| |
Private mstrFF As String Sub ColTable1Row1() Dim oFld As FormFields Dim i As Long Dim sCount As Long Dim sRow As Long Dim bProtected As Boolean Dim sPassword as String sPassword = "" 'Insert the password (if any), used to protect the form between the quotes With GetCurrentFF 'Establish which dropdown field is current mstrFF = GetCurrentFF.Name End With Set oFld = ActiveDocument.FormFields sCount = oFld(mstrFF).Result 'Get the dropdown field value sRow = Right(mstrFF, 1) 'Get the number at the end of the bookmark name 'Check if the document is protected and if so unprotect it If ActiveDocument.ProtectionType <> wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:=sPassword End If For i = 2 To 11 'Select each column of the active row in turn and colour the cell white ActiveDocument.Tables(1).Rows(sRow).Cells(i).Shading.BackgroundPatternColor = wdColorWhite Next i If sCount = 0 Then GoTo Quit 'If user enters 0, the row is already white so quit Select Case sRow Case 1 'Row 1 colour is red oCol = wdColorRed Case 2 'Row 2 colour is blue oCol = wdColorBlue Case 3 'Row 3 colour is gold oCol = wdColorGold Case 4 'Row 4 colour is green oCol = wdColorGreen Case Else End Select For i = 2 To sCount + 1 'Colour the cells in the row from column 2 to the number entered ActiveDocument.Tables(1).Rows(sRow).Cells(i).Shading.BackgroundPatternColor = oCol Next i Quit: 'Re-protect the form and apply the password (if any). If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=sPassword End If End Sub Private Function GetCurrentFF() As Word.FormField 'Get the dropdown field name Dim rngFF As Word.Range Dim fldFF As Word.FormField Set rngFF = Selection.Range rngFF.Expand wdParagraph For Each fldFF In rngFF.FormFields Set GetCurrentFF = fldFF Exit For Next End Function |
Thursday, September 8, 2011
Create a bar chart based on the content of a dropdown form field
Labels:
MACROS
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment