Iv. warm greeting card wizard for VBA Integrated Development instances
(1) Routine Decomposition
This example is a simple greeting card wizard that can run directly and help generate simple greeting cards. The main interface of the program is similar to the standard Wizard of word. You can directly jump to the corresponding step by clicking the "previous" and "Next" buttons or directly clicking the switch button on the left side of the form. Click the cancel button to close the wizard form, as shown in figure 4.
Click "Next" to switch to the next page. on this page, you can select a greeting card holiday and enter the name of the sender and recipient, as shown in Figure 5.
Click "Next" to switch to the next page. You can enter a congratulatory message, as shown in figure 6.
Click "Next" to switch to the last page of the Wizard program, and click "finish" to generate the document. The generated document contains the information you entered in the preceding steps to generate a simple greeting card.
(2) program knowledge points
1. How the wizard works
The essence of the Wizard program is the aforementioned Word template file (DOT file). The only difference is that its extension is "wiz". Therefore, to compile the Wizard program, you must first compile the template and debug the code, after debugging, change the extension to "wiz. The general steps for wizard compilation are as follows:
(1) automatic collection for creating the Wizard: the automatic collection for the wizard is an automatic collection containing bookmarks. When a file is generated, the pre-saved collection is called, the bookmarked object used for positioning in word can help the program locate and replace the object on the location where the entered content is required. This step is the key to compiling the Wizard program.
(2) Editing forms and modules: The Wizard program must use forms to implement interactive functions. Therefore, by using controls reasonably, you can design beautiful and easy-to-use forms, then, you can program events on the Form Controls to implement process control. After the form is designed, you must use the module to save common functions, including generating documents, replacing bookmarks, and deleting bookmarks.
(3) set the wizard entry: The Wizard must be triggered by a specific event. We recommend that you enter the code to call the wizard entry in the "new" event of the "thisdocument" object. Events of the Document Object include new events (triggered when a template-based document is created. Based on functional analysis, the Wizard program portal executes the event), Open events (triggered when the document is opened), and close events (triggered when the document is closed ).
2. interface design knowledge points
(1) The wizard needs to guide the user step by step. Therefore, we recommend that you use multipage controls to switch the interface.
(2) The VB-like control array function is not provided in the Form Design of VBA. Therefore, you need to access the controls collection object to control controls with similar appearances and functions.
(3) Since the Wizard program is essentially a template file from the aforementioned knowledge points, developers can directly open the wizard programs provided by word (such as "practical style wizard ), then, you can use the project resource manager to export the interface form, and then make reference and modifications on this basis, which greatly improves the compilation efficiency.
(4) controls set: the form's controls set represents all the controls it contains. Each control in the controls set has a unique index and can be referenced by the Control name. In the wizard, to effectively control controls with similar functions and appearances (such as switching buttons), you can use Name Reference to enumerate the controls set of access forms to control these controls.
(5) Multi-page control: multipage control is a container of pages sets. Each multi-page control contains one or more page objects. Its common attributes and methods are as follows:
Value Attribute: the default attribute of the Multi-page control. The index number of the current active page is returned, starting from "0" and so on.
Style attribute: used to set the display style of the Multi-page control label. It is recommended to set it to "2" in the Wizard (that is, do not display the header ).
(6) Page Object: This object represents the page of a Multi-page control. Different controls can be placed on each page object to achieve multi-interface switching. The default name of the first page object is "page1". Its common attributes and methods are as follows:
Picture attribute: used to set the background image of a page.
Picturetiling attribute: Used to set whether to tile an image.
Picturesizemode attribute: used to set the display mode of the background image.
(3) Procedures
1. Create the automatic collection required by the wizard
Using the preceding content, you can obtain the automatic collection required by the Wizard. Therefore, you must first enter the relevant text in the editing interface, and then add the bookmarks at the location where you want to insert the content, finally, select all content, name it "HK", and save it in the automatic image and text set of the template. 7:
2. Compile the form
Add a form to the project resource manager in the VBA editor and place controls such as multi-page controls, labels, text boxes, and lists (you can also design the form using the aforementioned import and modification methods ), because this form uses many controls, it is limited to space and is not described one by one. You can design it by yourself. This article only introduces some controls that need to be used in the code.
First, switch to the second page of the Multi-page control. The location and name of the control to be programmed on this page are as follows:
"Frmhkwiz" is the user form, "page1" is the Page Object of the Multi-page control, and "txtfsz" and "txtjsz" are both text box controls, "shpmap0" to "shpmap3", "lblmap0" to "lblmap3" are label controls, and "lstjr" is a list box control. Switch to the third page of the Multi-page control. The location and name of the control to be programmed on this page are as follows:
9. "page2" is the Page Object of the Multi-page control, "shpchartpath" is the label control, and "txthc" is the text box control, "export cancel", "cmdback", "release next", and "Release Finish" are both command button controls.
After the form interface is designed, switch to the code editing window. First, enter the constant declaration. The Code is as follows:
'Indicates the number of pages
Const p_count = "3"
'Constant of Tag Name
Const const_lbl = "lblmap"
'Constant of Tag Name
Const const_shp = "shpmap"
Enter the public variable, and the code is as follows:
'Variable used to save the current page number
Dim indexpanel as integer
Enter the custom Process Code as follows:
'Control initialization process
Private sub init_controls ()
With lstjr
. Additem "Christmas"
. Additem "Mid-Autumn Festival"
. Additem "National Day"
End
End sub
'Page switching process
Private sub changepage (inewpanel as integer)
If indexpanel = inewpanel or fwizardcallback then
Exit sub
End if
'Achieve page switching through the controls collection object
Frmhkwiz. Controls (const_shp & indexpanel). backcolor = vbwhite
Frmhkwiz. Controls (const_lbl & indexpanel). fontbold = false
Indexpanel = inewpanel
Frmhkwiz. Controls (const_shp & indexpanel). backcolor = vbgreen
Frmhkwiz. Controls (const_lbl & indexpanel). fontbold = true
Mpgwizardpage. value = indexpanel
End sub
Finally, enter the Event code of each control, as shown below:
'The tag click event used to simulate the switch button
Private sub lblmap0_click ()
Changepage (0)
End sub
Private sub lblmap1_click ()
Changepage (1)
End sub
Private sub lblmap2_click ()
Changepage (2)
End sub
Private sub lblmap3_click ()
Changepage (3)
End sub
Private sub shpmap0_click ()
Changepage (0)
End sub
Private sub shpmap1_click ()
Changepage (1)
End sub
Private sub shpmap2_click ()
Changepage (2)
End sub
Private sub shpmap3_click ()
Changepage (3)
End sub
Private sub shpmap4_click ()
Changepage (4)
End sub
'"Previous" button click event
Private sub cmdback_click ()
If indexpanel> 0 then
Changepage (indexpanel-1)
End if
End sub
'"Next" button click event
Private sub branch next_click ()
If indexpanel <p_count then
Changepage (indexpanel + 1)
End if
End sub
'"Cancel" button click event
Private sub functions cancel_click ()
'Close the form
Unload me
End sub
'"Done" button click event
Private sub register finish_click ()
Application. screenupdating = false
'The process of creating a document in the call module, that is, the interface process of the form and module
Createnewdoc (true)
End sub
'Form initialization event
Private sub userform_initialize ()
Indexpanel = 0
Mpgwizardpage. value = 0
Changepage (0)
Init_controls
End sub
3. Preparation Module
The module is used to save the common code in the Wizard program, add a standard module to the Project Resource Manager, name it "common", and enter the code (which can also mimic the import operation of the form, import the modules included in the Wizard provided by word, refer to and use many of the processes), as shown below:
'Start the wizard, that is, the entry to the Wizard program
Public sub startwizard ()
'Display form
Frmhkwiz. Show
End sub
'The process of creating a new document. The "fdummy" parameter can make the public-type process not appear in the word "macro" list.
Public sub createnewdoc (fdummy as Boolean)
Application. screenupdating = false
'Set the mouse to the "wait" shape
System. cursor = wdcursorwait
'Get the wizard Template
Set objwiztemplate = activedocument. attachedtemplate
Application. displayautocompletetips = true
'Insert the created automatic collection.
Activedocument. attachedtemplate. autotextentries ("HK"). Insert selection. Range, true
Activedocument. Select
'Call the process of replacing bookmarks and replace the content entered in the form in the document
Replacebookmark "Jr", frmhkwiz. lstjr. Text
Replacebookmark "fsz", frmhkwiz.txt fsz. Text
Replacebookmark "JSZ", frmhkwiz.txt JSZ. Text
Replacebookmark "HC", frmhkwiz.txt HC. Text
'Set attributes of the new document
With activedocument
. Spellingchecked = true
. Grammarchecked = true
. Undoclear
End
Application. displayautocompletetips = true
Selection. homekey wdstory
'Restore the default Mouse shape
System. cursor = wdcursornormal
Application. screenupdating = true
'Close the form
Unload frmhkwiz
'Call the process of deleting all bookmarks
Deleteallbookmark
End sub
'Process of replacing the bookmarked content
Private sub replacebookmark (which as string, what as string)
If Len (what) = 0 then
What = ""
End if
'Select the specified bookmarks.
Selection. goto what: = wdgotobookmark, name: = which
'Replace content
Selection. typetext what
End sub
'Process of deleting all bookmarks
Private sub deleteallbookmark ()
Dim BM as bookmark
'Traverse all bookmarks
For each BM in activedocument. bookmarks
'Delete bookmarks
BM. Delete
Next
End sub
4. Set the wizard Portal
After the code is compiled, switch to the "thisdocument" object in the project resource manager and enter the Event code, as shown below:
Private sub document_new ()
'Call the wizard process in the module (common)
Common. startwizard
End sub
5. Follow-up work
After the Wizard program is compiled and debugged correctly, you can change the extension to "wiz" and search for "normal. the folder where the dot "file is located, and place the Wizard program in this directory. After you start Word XP, the wizard icon is displayed in the" frequently used "column of the" template "form.