Content Control Event Firing randomly

Mattie Owens
Mattie Owens used Ask the Experts™
on
I got this script here, and I thought it was working but appears to run when other controls are clicked as well. Is there  solution. I only want the macro to run if I select either Item 1 or Item 2.

Sub MainMacro()
    Dim cc As ContentControl
    Set cc = ActiveDocument.ContentControls(1)
    Select Case cc.Range.Text
        Case "Item 1"
            Call Macro1
        Case "Item 2"
            Call Macro2
    End Select
End Sub

Sub Macro1()
    MsgBox "One"
End Sub

Sub Macro2()
    MsgBox "Two"
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
can you upload a sample document and indicate what you need to do within that document?

Author

Commented:
Here is a sample. I cannot upload the original.
Software Team Lead
Commented:
Set cc = ActiveDocument.ContentControls(1)

your code is fixed and selecting the first content control, are you intend to do so?

it's working fine if you call the ContentControlOnEnter event:

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    Call MainMacro
End Sub

Open in new window


in case you want to dynamically refer to the selected Content control for comparison, try modify codes to:

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    Call MainMacro(ContentControl)
End Sub

Open in new window


then for your module:

Sub MainMacro(ByRef cc As ContentControl)
    Select Case cc.Range.Text
        Case "Item 1"
            Call Macro1
        Case "Item 2"
            Call Macro2
    End Select
End Sub

Sub Macro1()
    MsgBox "One"
End Sub

Sub Macro2()
    MsgBox "Two"
End Sub

Open in new window

Author

Commented:
Learning so much. Thanks for your help again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial