Link to home
Start Free TrialLog in
Avatar of cksm4
cksm4

asked on

ContentControlOnExit

I have a template where I need the ContentControlOnExit to run on the active document. It words on the template itself, but not a copy.

Private Sub Document_ContentControlOnExit(ByVal contentcontrol As contentcontrol, Cancel As Boolean)

Open in new window


I am thinking I need an event handler?

Thanks,
Brock
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

The event will fire if the content control is in the Template, or it it is in a document created from it in the usual way (e.g. via File - or Office button -  /New),  provided that the document still has access to the template.
Avatar of cksm4
cksm4

ASKER

Hello Graham,

Here is my code:

Dim lastCC As ContentControl

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

On Error Resume Next
Validate lastCC, False

End Sub

Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)

Set lastCC = ContentControl

End Sub

Private Sub Validate(ByVal CC As ContentControl, Cancel As Boolean)

Application.ScreenUpdating = False

If CC.Tag = "Company" Then
        If CC.Range.Text = "Company A*" Or CC.Range.Text = "Company B" Then
            If Not ActiveDocument.Bookmarks.Exists("CompanySection") = True Then
                For Each ContentControl In ActiveDocument.ContentControls
                    If ContentControl.Type = wdContentControlGroup Then
                        ContentControl.Ungroup
                    End If
                Next
                Selection.GoTo What:=wdGoToBookmark, Name:="AddAdditionalSection"
                Selection.MoveUp Unit:=wdLine, Count:=1
                For Each mytemplate In Templates
                    If mytemplate.Name = "Refme.dotm" Then _
                        mytemplate.BuildingBlockEntries("AddAdditionalSection").Insert Where:=Selection.Range, RichText:=True
                Next
                Dim oRng As Word.Range
                Selection.GoTo What:=wdGoToBookmark, Name:="AddCmdButton"
                Set oRng = Selection.Range
                Dim shp As Word.InlineShape
                Dim sCode As String
                oRng.Collapse wdCollapseEnd
                Set shp = oRng.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
                With shp.OLEFormat.Object
                    .Caption = "Delete Contact"
                    .Name = "CmdDeleteContact"
                    .AutoSize = True
                    .Enabled = False
                End With
                oRng.Collapse wdCollapseEnd
                Set shp = oRng.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
                With shp.OLEFormat.Object
                    .Caption = "Add Contact"
                    .Name = "CmdAddContact"
                    .AutoSize = True
                End With
                With ActiveDocument
                    Selection.WholeStory
                    Set CC = Selection.Range.ContentControls.Add(wdContentControlGroup)
                    Selection.HomeKey
                End With
            End If
        ElseIf CC.Range.Text <> "Company A" Or CC.Range.Text <> "Company B"Then
            MsgBox "True1"
            If ActiveDocument.Bookmarks.Exists("AdditionalSection") = True Then
            MsgBox "True2"
                For Each ContentControl In ActiveDocument.ContentControls
                    If ContentControl.Type = wdContentControlGroup Then
                        ContentControl.Ungroup
                    End If
                Next
                ActiveDocument.Bookmarks("AdditionalSection").Select
                Selection.Rows.Delete
                With ActiveDocument
                    Selection.WholeStory
                    Set CC = Selection.Range.ContentControls.Add(wdContentControlGroup)
                    Selection.HomeKey
                End With
            End If
        End If
End If

End Sub

Open in new window


Here is an example of my issue from another user: http://stackoverflow.com/questions/5268676/word-vba-events-only-fire-once-for-documents-based-on-a-template

FYI: I used the validate procedure to address the onexit bug which causes the onenter to also fire. I also add the two command buttons through VBA because when I add them to the quick part (where they end up) they lose their reference to the code.

Any help would be greatly appreciated!

Brock
The link that you give is about a programming error where the code in the template referred to ThisDocument (i.e. the template) instead of the ActiveDocument, so it didn't do what was expected.

Is the code in the document or the template? If it is in the template, is the template still the attached template for the document?

Avatar of cksm4

ASKER

The code is in the template and the document is still attached to the template. The macro runs every time when ran in the template. When ran from documents created from the template it runs only once. I removed most of the code in order to determine what was causing the code to stop:

If CC.Tag = "Company" Then
        If CC.Range.Text = "Company A*" Or CC.Range.Text = "Company B" Then
             MsgBox "True1"   
        ElseIf CC.Range.Text <> "Company A" Or CC.Range.Text <> "Company B"Then
            MsgBox "True2"
       End if
End If 

Open in new window


It runs every time when I do this. This is what makes me think that something is causing the macro to focus on the template and not the document after it runs... which is why I was thinking there was a fix somewhere in that link :/

I am thinking I need the link the macro back to the active document.

Brock
I, too, can reproduce the problem with your main code, but not with a much simpler test. I haven't yet pinned it down to any particular instruction.
Avatar of cksm4

ASKER

If I press the stop button in Visual Basic it temporarily clears the problem and will let the macro run once more. However, I still cannot find the actual error and have tried uncessfully to trap it.

Brock
Avatar of cksm4

ASKER

Would it be possible to reset Visual Basic after the macro runs? Not optimal... but a work around.
The End instruction is supposed to do the same as the Stop button, but I think we are working around some sort of bug, so that couldn't be guaranteed to work.
ASKER CERTIFIED SOLUTION
Avatar of cksm4
cksm4

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cksm4

ASKER

No solution provided