none
How to implement a .exitmacro RRS feed

  • Question

  • I would like to cause a macro to run after the user inputs to the last content control on a form. The sample given in the MSDN library seem straight forward and I know how to create the macro.  I just can't figure out where the code in the example is supposed to reside...

    ExitMacro                         

    Example

    This example assigns the macro named "Reformat" to the first form field in the selection.

    If Selection.FormFields.Count > 0 Then _
        Selection.FormFields(1).ExitMacro = "Reformat"

    Wednesday, June 11, 2014 5:17 PM

Answers

  • Hi rkferg

    The "enter" and "exit" macros for content controls need to be in the ThisDocument module of the document or template.

    Double-click the ThisDocument entry in the Word Objects folder to display the page where you type the code.

    From the list on the left of the code window, choose Document. From the list on the right, choose ContentControlOnExit.

    This event will fire for ALL content controls in the document. If you need to have it run for only certain ones, query the ContentControl argument passed to the event. You can query any property, but usually you'd work with the Title or Tag property to identify the content control and take action. For example:

    Select Case
      Case Is ContentControl.Title = "LastContentControl"
       If Selection.FormFields.Count > 0 Then 
         Selection.FormFields(1).ExitMacro = "Reformat"
          End If
      Case Else
    End Select


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, June 11, 2014 7:07 PM
    Moderator

All replies

  • Hi rkferg

    The "enter" and "exit" macros for content controls need to be in the ThisDocument module of the document or template.

    Double-click the ThisDocument entry in the Word Objects folder to display the page where you type the code.

    From the list on the left of the code window, choose Document. From the list on the right, choose ContentControlOnExit.

    This event will fire for ALL content controls in the document. If you need to have it run for only certain ones, query the ContentControl argument passed to the event. You can query any property, but usually you'd work with the Title or Tag property to identify the content control and take action. For example:

    Select Case
      Case Is ContentControl.Title = "LastContentControl"
       If Selection.FormFields.Count > 0 Then 
         Selection.FormFields(1).ExitMacro = "Reformat"
          End If
      Case Else
    End Select


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, June 11, 2014 7:07 PM
    Moderator
  • Hi Cindy,

    Thanks for the help.  Can I infer from your example that content controls are order dependent, i.e.  if there are ten controls dispersed throughout a document, to the compiler or code, they appear in sequential order, top to bottom/left to right, such that the last control reference would be Selection.FormFields(10).ExitMacro ... ?

    Fergie

    Thursday, June 19, 2014 2:32 PM
  • HI Fergie

    Well, my example doesn't do anything with content controls, really - it bases on YOUR example with form fields... (Those are two completely different things!)

    However, Word "sees" everything in a document from left-to-right, top-to-bottom and that's how things will be indexed.

    In this particular case, however, my code is picking up the first content control IN THE CURRENT SELECTION, not in the document. If a content control is selected and contains form fields, then this picks up the first form field in the content control.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, June 19, 2014 5:57 PM
    Moderator