Friday, July 23, 2010

How to retrieve value from Content Controls using Word VBA

The following snippet validates the user selection using VBA. This code uses the content control created in previous example - (How to add Content Controls using VBA)

Sub Validate_ContentControl()

Dim oCC As ContentControl
Dim OCCEntry As ContentControlListEntry

Set oCC = ActiveDocument.ContentControls(1)

For i = 1 To oCC.DropdownListEntries.Count
     If oCC.DropdownListEntries.Item(i).Text = oCC.Range.Text Then
        Set OCCEntry = oCC.DropdownListEntries.Item(i)
        ' Check the text against value - can be checked directly with text
        If OCCEntry.Value = 1 Then
            MsgBox "Correct"
        Else
            MsgBox "Try Again"
            Exit Sub
        End If
     End If
    
Next i

3 comments:

  1. Anonymous10:08 PM

    It is very helpful to me. Thanks!

    ReplyDelete
  2. Anonymous4:23 AM

    I have a Problem it Comes an error at this line:

    If oCC.DropdownListEntries.Item(i).Text = oCC.Range.Text Then


    What could be wrong ?

    regards skyerjoe


    ReplyDelete
  3. john s.5:41 AM

    Doesn't work word marked the "If oCC.DropdownListEntries.Item(i).Text = oCC.Range.Text Then"

    so i think something doesn't fit

    regards

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group