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.
I am thinking I need an event handler?
Thanks,
Brock
Private Sub Document_ContentControlOnExit(ByVal contentcontrol As contentcontrol, Cancel As Boolean)
I am thinking I need an event handler?
Thanks,
Brock
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.
ASKER
Hello Graham,
Here is my code:
Here is an example of my issue from another user: http://stackoverflow.com/q uestions/5 268676/wor d-vba-even ts-only-fi re-once-fo r-document s-based-on -a-templat e
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
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
Here is an example of my issue from another user: http://stackoverflow.com/q
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?
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?
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:
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
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
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.
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
Brock
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No solution provided