WD98: How to Create and Display a Custom Dialog Box

Last reviewed: March 13, 1998
Article ID: Q182426
The information in this article applies to:
  • Microsoft Word 98 Macintosh Edition

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 engineers 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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:

  1. On the Tools menu, point to Macro, and then click Visual Basic Editor.

  2. In the Visual Basic project window, click Normal.

    By clicking Normal, the form will be 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 will be available only when the custom template is open, available as an add-in, or attached to an open document.

  3. On the Insert menu, click UserForm.

  4. 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.

  5. 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.

  6. Select the first command button control and then click Code on the View menu.

  7. 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.
    
    

  8. On the View menu, click Object to return to the form.

  9. Select the second command button control and then click Code on the View menu.

  10. 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.
    
    

  11. On the File menu, click Save Normal.

Displaying the Form

To display the form, switch to Word, and then do the following:

  1. On the Tools menu, point to Macro, and then click Macros.

  2. In the Macro Name box, type FillInForm and then click Create.

  3. In the Code window, type "UserForm1.Show" (without the quotation marks), so that your code looks like this:

         Sub FillInForm()
    
            UserForm1.Show
         End Sub
    
    

  4. Save and close the macro.

  5. Close the Visual Basic Editor.

  6. You can now run the macro from Word to display the form.

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:

  1. Double-click the form.

  2. From the Object list, select UserForm.

  3. From the Procedure list, select Initialize.

  4. In the Code window, type the following code

          Private Sub UserForm_Initialize()
    
             TextBox1.Text = "<default text>"
          End Sub
    
       where <default text> is the text you want to appear in the text box when
       the form appears.
    
    
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:

   ARTICLE-ID: Q176476
   TITLE     : OFF: Office Assistant Not Answering Visual Basic Questions

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

   ARTICLE-ID: Q181058
   TITLE     : OFF98: How to Run Sample Code from Knowledge Base Articles

REFERENCES

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

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications


Additional query words: vb vba vbe
Keywords : kbcode kbprg kbdta kbdtacode kbmacroexample OffVBA macword98
Version : MACINTOSH:98
Platform : MACINTOSH
Hardware : MAC
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.