HOWTO: Show a Modal Form from an OLE Server DLL

Last reviewed: August 4, 1997
Article ID: Q172059
The information in this article applies to:
  • Enterprise Edition of Microsoft Visual Basic for Windows, 32-bit only, version 4.0

SUMMARY

Showing a modal form from an out-of-process OLE Server is generally not recommended. One reason is that the modal form may be displayed behind the client program, making it difficult for the user to see that it needs to be addressed. Another problem is that while the method of the OLE Server is still processing, the client program may not handle Paint messages, which can cause the client screen to become cluttered by the images of other windows that have overlaid it.

Finally, if an OLE Server is just performing a service and is not responsible for any of the user interface, it is more appropriate to raise an error and return that error to the client program than for the OLE Server itself to show a message box.

MORE INFORMATION

One way to show a modal form from a client program is to use an OLE DLL as the server application. The DLL will run in the address space of the client. Consequently, any forms shown by the OLE DLL will automatically have the client program's form as a parent.

The steps below outline how to create an out-of-process OLE Server DLL that shows a modal form inside Excel 7.0.

Step-by-Step Example

  1. Start a new project. Form1 is created by default.

  2. On Form1, place a CommandButton, and place the following code in the CommandButton:

          Private Sub Command1_Click()
          Unload Me
          End Sub
    

  3. Insert a class module by choosing Insert...Class Module from the menu.

  4. In the Class module, place the following code:

          Public Sub ShowModal()
          Form1.Show 1
          End Sub
    

  5. Press the F4 key to bring up the Properties window for the class, and change the Name of the class to clsShowForm. Set Instancing to Createable MultiUse, and set Public to True.

  6. From the Tools menu, choose Options, and select the Project tab. Change the project name to prjShowForm, select OLE Server, and change the Application Description to "Show Project Example" (without the quotes).

  7. Also in the Project tab, switch the Startup Form to Sub Main(). Click OK in the Options dialog box.

  8. Add a regular module to the project by choosing Module from the Insert menu.

  9. Place a main subroutine into it:

          Sub main()
          
          End Sub
    
    

  10. From the Filemenu, choose Make OLE DLL to make the OLE Server. This

        creates the DLL and registers the OLE Server on the system.
    

  11. Start Excel, and by default it will start in Book1. This is important
because the example above uses the Caption of Excel to find the window handle. From the Insert menu, choose Macro Module to go into the VBA editor.

  1. Place the following code in the editor:

          Sub test()
          Dim x As Object
          Set x = CreateObject("prjShowForm.clsShowForm")
          x.ShowModal
          End Sub
    
    
Run the code in the Excel editor and it will show the modal form provided by the OLE Server DLL. It will be shown modally in front of Excel until the form is dismissed. If the same form is shown from an out-of-process OLE Server, two undesirable characteristics will manifest themselves: Excel will fail to paint as the modal form is moved around and it will be possible to click on Excel to bring it to the foreground.
Keywords          : PrgOther VB4ALL VB4WIN kbhowto
Technology        : kbole
Version           : 4.0
Platform          : WINDOWS


================================================================================


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: August 4, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.