Word VBA Set Control Calls to Macro

gijimbo

Board Regular
Joined
Feb 3, 2010
Messages
130
I'm trying to create trip scheduler template. On this template (towards the top) I've got a table with 7 columns for data:
Appointment Type
Name
Date
Start Time
End Time
Start Location
End Location

There are 2 header rows; one for the title and the other for the categories listed above. After that, there is one row with a drop-down control box in the first cell. I currently have a macro set to run on field exit. This macro makes a call to 1 of 3 user forms that collect collect data based on the selected drop-down value. This all works just fine.

I also have a button that runs another macro which adds another row at the bottom of the table and fills the first cell in the row with another drop-down control box (with the same 3 options as the other). This also works fine except I can't find how to apply the "run macro on exit" property (if it even exists).

Can anyone help me? Thanks in advance.

Code:
Code:
Sub AddApt_Click()

' Add Appointment macro
'

With ActiveDocument
    'Remove protection if protected
    If .ProtectionType = wdAllowOnlyFormFields Then .Unprotect
    
    With .Tables(1)
        'Add new row on bottom
        .Rows.Add
        'Add appointment type drop down box
        .Range.FormFields.Add _
            Range:=.Rows.Last.Cells(1).Range, _
            Type:=wdFieldFormDropDown
        .Rows.Last.Range.FormFields(1).Name = "Appointment" & CStr(.Rows.Count - 2)
        With .Rows.Last.Range.FormFields(1).DropDown.ListEntries
            .Add Name:="Meeting"
            .Add Name:="Flight"
            .Add Name:="Hotel"
        End With
    End With
    
    'Re-protect
    .Protect _
        Type:=wdAllowOnlyFormFields, _
        NoReset:=True
End With


End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try:
Code:
Sub AddApt_Click()
' Add Appointment macro
With ActiveDocument
'Remove protection if protected
If .ProtectionType = wdAllowOnlyFormFields Then .Unprotect
With .Tables(1)
  'Add new row on bottom
  .Rows.Add
  'Add appointment type drop down box
  .Range.FormFields.Add _
    Range:=.Rows.Last.Cells(1).Range, _
    Type:=wdFieldFormDropDown
  With .Rows.Last.Range.FormFields(1)
    .Name = "Appointment" & CStr(.Rows.Count - 2)
    .CalculateOnExit = True
    With .DropDown.ListEntries
      .Add Name:="Meeting"
      .Add Name:="Flight"
      .Add Name:="Hotel"
    End With
  End With
  'Re-protect
  .Protect _
    Type:=wdAllowOnlyFormFields, _
     NoReset:=True
End With
End Sub
 
Upvote 0
Thanks but that's not really the problem I'm having. I see how what I said could be interpreted that way though. I'll try to explain the problem more clearly.

It's not that the code's not running, it's that I don't even know how to assign a macro to the field using vba code. The only way I know how is to right click on the field in the document and set it using the properties dialog.

Is there a way to assign a macro to the "run macro on exit" section of the field properties through vba?
 
Upvote 0
In that case, change the line:
.CalculateOnExit = True
to:
.ExitMacro = "yourmacro"
 
Upvote 0
Wow! I can't believe it was that simple. Thanks a ton. Actually I can't believe I spent most of the day yesterday doing google and msdn searches without finding anything that mentioned the ExitMacro property. I suppose I just had a bad day for searching.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top