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
cksm4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
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.
0
cksm4Author Commented:
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
0
GrahamSkanRetiredCommented:
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?

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

cksm4Author Commented:
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
0
GrahamSkanRetiredCommented:
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.
0
cksm4Author Commented:
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
0
cksm4Author Commented:
Would it be possible to reset Visual Basic after the macro runs? Not optimal... but a work around.
0
GrahamSkanRetiredCommented:
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.
0
cksm4Author Commented:
No solution, so I am closing this question
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cksm4Author Commented:
No solution provided
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.