In Word, we can easily specify a user-defined procedure for a word control, complete or transfer (disabled) by modifying the Word command's method. The corresponding built-in way. Its principle is to use the corresponding macro name to replace the process of the method. In the following three sections, we can experience the similarities and differences.
Sub Example ()
Dim I as CommandBarControl
For all I in Application.CommandBars.FindControls
If i.id = 4 Then
I.onaction = "MySub" ' specifies the name of the macro
End If
Next
End Sub
Sub Resetsub ()
Dim I as CommandBarControl
For all I in Application.CommandBars.FindControls
If i.id = 4 Then ' id=4 ' equals ctrl+p (File/print)
I.onaction = "' Restores the original ID function
End If
Next
End Sub
Private Sub Document_Close ()
Resetsub ' resume after closing the document
End Sub
Private Sub Document_Open ()
Example ' Modify
End Sub
The above is the first part, the following is the second part:
Sub FilePrint ()
MySub
End Sub
Sub mysub ()
MsgBox "Cannot use Print feature!"
End Sub
This procedure is a common part
In Excel, we can only specify a macro process for an existing program to modify the command through the FindControls (ID) method, which we may also do in Word, but if we take the second part, it is simpler This is called modifying the word command. Of course a process named mysub can be omitted and written directly in the FilePrint macro. The following is a practical example of modifying Word commands (This example is in a custom template)
Sub fileprint () ' Modify Word command (File/print: ctrl+p)
Dim Pc As Integer, Var as Integer
With Application.dialogs (wdDialogFilePrint)
If. Show =-1 Then
Pc =. Numcopies ' Get the number of copies printed
Var = Me.variables ("Printpagecount"). Value ' extends the previous number of printed copies
Me.variables ("Printpagecount"). Value = Pc + Var ' Today's total number of printed sheets
Me.save ' Save
MsgBox "The current cumulative number of copies is" & Me.variables ("Printpagecount"). Value
End If
End With
End Sub
Sub fileprintdefault ' modify Word command (commonly used toolbar/print active document)
ActiveDocument.PrintOut ' default print
Me.variables ("Printpagecount"). Value = _
Me.variables ("Printpagecount"). Value + 1
Me.save ' Save
MsgBox "The current cumulative number of copies is" & Me.variables ("Printpagecount"). Value
End Sub
Private Sub Document_Open ()
On Error Resume Next
Me.Variables.Add name:= "Printpagecount" ' Book a Document variable
End Sub
If you quickly get the command name for a command in Word, there are several ways to make the word command in the Macro dialog box, we can know all Word commands, and you can use the Listcommands command in the Macro dialog box to automatically list all Word commands; can also be obtained by using custom/command/All commands, or by inserting a list of macro names in the domain/macrobutton domain; The most convenient is to use the "+" number on the ctrl+alt+ numeric keypad, and when the cursor becomes "Chinese knot", click the button you want, A custom dialog box appears, and the command displayed in this dialog box is the name of the command you want.