Word模板开发:能否通过单击/双击Content Control唤起Userform?
Absolutely! You can definitely trigger a UserForm by clicking or double-clicking a Content Control in Word VBA—let’s break down the feasible approaches step by step:
1. Click Trigger (Using Native Word Events)
Word has a built-in event that fires when you click into a Content Control, making this approach simple and reliable. Here’s how to set it up:
- Open the VBA Editor in Word (press
Alt + F11). - In the Project Explorer, locate your template document and double-click
ThisDocumentto open its code module. - Paste this code, adjusting the control identifier and UserForm name to match your project:
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl) ' Target the specific Content Control using its Title (or Tag) If ContentControl.Title = "LaunchUserForm" Then ' Replace "UserForm1" with your actual UserForm's name UserForm1.Show End If End Sub - Save your template as a
.dotm(Macro-Enabled Template) to preserve the code for future use.
How it works: Every time you click into the Content Control with the matching Title, the ContentControlOnEnter event fires and launches your UserForm.
Pro tip: Assign a unique Title or Tag to your trigger control to avoid accidentally launching the form from other Content Controls in your template.
2. Double-Click Trigger (API-Assisted Approach)
Word doesn’t have a native double-click event for Content Controls, but we can work around this using Windows API functions to detect double-clicks by measuring the time between consecutive clicks. Here’s the implementation:
Step 1: Declare API Functions
First, insert a new standard module (right-click your project in the VBA Explorer > Insert > Module) and paste these declarations (compatible with both 32-bit and 64-bit Word):
Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long Declare PtrSafe Function GetDoubleClickTime Lib "user32" () As Long ' Track click timing and target control Dim lastClickTime As Long Dim lastClickedControl As ContentControl
Step 2: Add Double-Click Detection Logic
Return to the ThisDocument module and add this code:
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl) Dim currentClickTime As Long currentClickTime = GetTickCount ' Check if this is a double-click: same control + time gap < system double-click threshold If currentClickTime - lastClickTime < GetDoubleClickTime And _ Not lastClickedControl Is Nothing Then If lastClickedControl.Title = ContentControl.Title Then ' Launch your UserForm UserForm1.Show ' Reset values to prevent repeated triggers lastClickTime = 0 Set lastClickedControl = Nothing Exit Sub End If End If ' Update tracking variables for the next click lastClickTime = currentClickTime Set lastClickedControl = ContentControl End Sub
How it works: Every time you click into a Content Control, we record the time and the control. If the next click happens within the system’s default double-click time (usually ~500ms) and is on the same control, we trigger the UserForm.
Key Notes
- Test the double-click timing—using
GetDoubleClickTimeensures it matches the user’s system settings, but you can adjust the threshold if needed. - Just like the click trigger, use a unique Title/Tag for your target control to avoid false positives.
内容的提问来源于stack exchange,提问作者Faiz

