Share via

Document_ContentControlOnExit for different bookmarks

Anonymous
2022-06-10T14:37:53+00:00

Split from this thread.

Essentially, you are trying to have the content in one spot depend on a choice made in a different spot, but not be the same as that choice, so you are not repeating data, exactly.

Using a Building Blocks Gallery Content Control. is going to be tough. It would be simple y to map the control and reuse everything in it, but you just want the price, correct?

If you are only going to have one choice, I think you could place the price in a bookmark in the Building Block and use a Ref field for the price at the end. You would need a macro that runs upon using the selection that updates that Ref field. The reason for the limitation of one choice is that you can only use a bookmark name in a document once. Each of your building blocks must use the same bookmark name.

Until you have picked one of the choices, there will not be the bookmark and so you will have an error message at the Ref field.

Assuming you are using a Ref Field, you would want to update it using a macro.

Here is a macro that would go in the ThisDocument part of your template's code. If the Content Control is title"Choose Product" then it will update any Ref field that has the word "price" in it when you exit the Content Control.

Private Sub Document_ContentControlOnExit(ByVal ContentControl _



         As ContentControl, Cancel As Boolean)



    ' Greg Maxey and Charles Kenyon and Jay Freedman



    ' https://gregmaxey.com/word_tip_pages/validate_content_control_entries.html



    '



    If ContentControl.Title = "Choose Product" Then



    On Error Resume Next



    Dim rg     As Range



    Dim fld    As Field



    Set rg = ActiveDocument.Range



    rg.TextRetrievalMode.IncludeFieldCodes = True



    For Each fld In rg.Fields



        If fld.Type = wdFieldRef And InStr(fld.Code.Text, "Price") Then fld.Update



    Next fld



    Set rg = Nothing



    Set fld = Nothing



    On Error GoTo -1



    End If



End Sub

Hello Charles,

After having successfully implemented this macro, I now want to use an exact same code for the same CC. Of course, this time I am using another bookmark. However, when I copy the code you mentioned previously, it gives me the notification that the name Document_ContentControlOnExit is ambiguous. That makes sense, as it uses the exact same name as for the previous code.

What should I change the name to? I noticed that when I change the name, it is not grouped under "Document" object. It changes to (General). Same goes for the ContentControlOnExit event. Any changes in the name seem to not make the code work as intended anymore.

I already changed the name to Document2_ContentControlOnExit as a test. The bookmark works, but only when pressing F9. I want to have it calculate on exit of the CC.

I have looked at this website Validate Content Controls Entries (gregmaxey.com), but it seems to be a different situation.

What should I change the name to? Or how can I apply the exact same code for a different bookmark within in the same CC?

Thank you a lot!

Microsoft 365 and Office | Word | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2022-06-10T14:59:20+00:00

    DO NOT change the name of the macro. There should be only one ContentControlOnExit routine, and it should have the Document_ prefix, not anything else. This macro is an "event handler", one that runs whenever Word detects that the active cursor is about to exit from any content control in the active document.

    The proper way to work with multiple content controls in the same document is to determine which content control is being exited, and do whatever is necessary for that control. The identity of the current content control is passed into the macro in the parameter named "ContentControl" (shown in bold here).*

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

    The usual way to set up for multiple controls is to give each content control in the document a unique title and/ or tag value in its Properties dialog. Then the beginning of the code in the ContentControlOnExit routine can do something like this:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) 
    
        Select Case ContentControl.Tag 
    
            Case "Name" 
    
                'do something with the Name control 
    
            Case "Address" 
    
                'do something different with the Address control 
    
            Case "Phone" 
    
                'do something else with the Phone control 
    
            Case Else 
    
                ' do nothing 
    
                Exit Sub 
    
        End Select 
    
    End Sub 
    

    A different circumstance is when you have one set of controls that all need to be treated the same way, and another set of controls in the same document that don't need to be treated at all. Then the first set should all have the same tag value, and the other set don't need to have any tag value. The test at the start of the ContentControlOnExit routine can be simply

        If ContentControl.Tag <> "treat_this" Then Exit Sub 
    
        ' if the tag is "treat_this", then execute the rest of the code 
    

    ___

    * I don't like the practice of naming a variable the same as a standard data type, but this is the way VBA automatically creates the ContentControl event handlers. I prefer to rename the variable to just "CC",

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

    and then use CC as the variable in the rest of the code.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2022-06-10T14:41:09+00:00

    Bart, because you posted this at the end of a question marked as Answered, it is unlikely to get the response you need. I am splitting this into a new question so it will be more likely to get response from people who understand this better than I do.

    You only use the one macro. It can check the title or tag of the content control and vary the bookmark based on the name or tag of the content control. The one macro responds to all content controls in the document.

    Was this answer helpful?

    0 comments No comments