-
Another way is to store related values in document variables:
[vba]Option Explicit
Private Type ListData
pType As String
pColor As String
End Type
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim tData As ListData
Select Case CC.Tag
Case Is = "ddTest"
tData = GetData(CC.Range.Text)
With ActiveDocument
.SelectContentControlsByTitle("Type").Item(1).Range.Text = tData.pType
.SelectContentControlsByTitle("Color").Item(1).Range.Text = tData.pColor
End With
Case Is = "ddTestII"
tData = GetDataII(CC.Range.Text)
With ActiveDocument
.SelectContentControlsByTitle("TypeII").Item(1).Range.Text = tData.pType
.SelectContentControlsByTitle("ColorII").Item(1).Range.Text = tData.pColor
End With
End Select
End Sub
Private Function GetData(pText As String) As ListData
Dim oRng As Word.Range
Dim oTbl As Word.Table
Dim lngIndex As Long
Set oTbl = ActiveDocument.Tables(1)
Set oRng = oTbl.Range
With oRng.Find
.ClearFormatting
.Text = pText
.Execute
If .Found = True Then
lngIndex = CLng(Left(oRng.Cells(1).Previous.Range.Text, Len(oRng.Cells(1).Previous.Range.Text) - 2))
End If
End With
If lngIndex > 0 Then
GetData.pType = Left(oTbl.Cell(lngIndex, 3).Range.Text, Len(oTbl.Cell(lngIndex, 3).Range.Text) - 2)
GetData.pColor = Left(oTbl.Cell(lngIndex, 4).Range.Text, Len(oTbl.Cell(lngIndex, 4).Range.Text) - 2)
Else
GetData.pType = ""
GetData.pColor = ""
End If
Set oTbl = Nothing
Set oRng = Nothing
End Function
Private Function GetDataII(pText As String) As ListData
Dim arrData() As String
arrData() = Split(ActiveDocument.Variables(pText).Value, "|")
GetDataII.pType = arrData(0)
GetDataII.pColor = arrData(1)
Exit Function
Err_NoPick:
GetDataII.pType = ""
GetDataII.pColor = ""
End Function
'Setup the variables in advance
Sub StoreVariables()
Dim oVars As Variables ', oVar As Variable
Set oVars = ActiveDocument.Variables
'For Each oVar In oVars
' oVar.Delete
'Next oVar
With oVars
.Add "Apples", "Fruit|Red"
.Add "Carrots", "Vegetable|Orange"
.Add "Corn", "Grain|Yellow"
.Add "Blueberries", "Fruit|Blue"
.Add "Limes", "Fruit|Green"
End With
Set oVars = Nothing
End Sub
[/vba]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules