Microsoft KB Archive/164815

= WD: How to Create and Display a Custom Dialog Box =

Article ID: 164815

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q164815





SUMMARY
This article contains step-by-step instructions for creating and displaying a Visual Basic for Applications custom dialog box (User Form).



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Creating the User Form
The following example creates a user form with a TextBox control and two CommandButton controls (an "OK" button and a "Cancel" button).

Create the form using the following steps:  On the Tools menu, point to Macro, and then click Visual Basic Editor. In the Visual Basic project window, click Normal.

Clicking Normal makes the form available to all documents. To create the user form in a custom template, make sure the template is currently open and select the template project name instead of Normal. For example, if the custom template name is "My Template", in the Visual Basic project window select "TemplateProject(My Template)". The user form is available only when the custom template is open, available as an add-in, or attached to an open document. On the Insert menu, click UserForm. Click the TextBox control on the Toolbox toolbar and then click the form.

The TextBox control appears in the default size. Drag a sizing handle to resize the control, or drag the control to move it to a new location. Click the CommandButton control on the Toolbox toolbar, and then click the form.

The CommandButton control appears in the default size. Drag a sizing handle to resize the control, or drag the control to move it to a new location. Repeat this step to add a second CommandButton control. Select the first command button control, and then click Code on the View menu.</li>  In the Code window, type "Selection.TypeText TextBox1.Text" (without the quotation marks), so that your code looks like this: Private Sub CommandButton1_Click Selection.TypeText TextBox1.Text End sub NOTE: When you click this button on the form, the contents of the text box is inserted into the active document at the location of the insertion point. </li> On the View menu, click Object to return to the form.</li> Select the second command button control, and then click Code on the View menu.</li>  In the Code window, type "End" (without the quotation marks), so that your code looks like this: Private Sub CommandButton2_Click End End sub When you click this button after you click the first command button, the form is closed. If you click this button instead of the first command button, the form is canceled. </li> On the File menu, click Save Normal.</li></ol>

Displaying the Form
To display the form, switch to Word, and then do the following: <ol> On the Tools menu, point to Macro, and then click Macros.</li> In the Macro Name box, type FillInForm and then click Create.</li>  In the Code window, type "UserForm1.Show" (without the quotation marks), so that your code looks like this: Sub FillInForm UserForm1.Show End Sub </li> Save and close the macro.</li> Close the Visual Basic Editor.</li> You can now run the macro from Word to display the form.</li></ol>

If you want default text to appear in the text box, create an "initialize" event for the UserForm object. To do this, use the following steps: <ol> Double-click the form.</li> From the Object drop-down list, select UserForm.</li> From the Procedure drop-down list, select Initialize.</li>  In the Code window, type the following code: Private Sub UserForm_Initialize TextBox1.Text = " " End Sub where is the text you want to appear in the text box when the form appears. </li></ol>

For more information about User Forms, click the Office Assistant while in the Visual Basic Editor, type "UserForm," click Search, and then click to view "UserForm Object."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

176476 OFF: Office Assistant Not Answering Visual Basic Questions

For additional information, please see the following article in the Microsoft Knowledge Base:

173707 OFF97: How to Run Sample Code from Knowledge Base Articles

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

163435 VBA: Programming Resources for Visual Basic for Applications

Additional query words: wordcon vb vba vbe

Keywords: kbdtacode kbhowto kbprogramming KB164815

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.