Microsoft KB Archive/221195

{|
 * width="100%"|

Sample: OFramer.exe Automates Excel and Word Inside a VB Form

 * }

Q221195

-

The information in this article applies to:


 * Microsoft Visual Basic Learning Edition for Windows, version 6.0
 * Microsoft Visual Basic Professional Edition for Windows, version 6.0
 * Microsoft Visual Basic Enterprise Edition for Windows, version 6.0
 * Microsoft Office XP Developer
 * Microsoft Office 2000 Developer
 * Microsoft Excel 97 for Windows
 * Microsoft Word 97 for Windows

-

SUMMARY
OFramer.exe (Office Framer) is a sample that demonstrates an alternative approach to automating Microsoft Excel and Microsoft Word from Visual Basic 6.0. Rather than use the OLE control on a form, Office Framer displays the actual automation server inside a frame control on the form. This is primarily accomplished by using the SetParent API function to change the parent of the automation server to the frame control.

This approach offers the following advantages:


 * The user interface of the automation server is available in the frame. This includes menus, toolbars, scrollbars, and so forth.
 * There is more flexibility through automation to manipulate the appearance of the automation server, such as hiding user interface elements and controlling the window size.
 * The automation server is private to the form. If the user interactively starts Excel or Word, this does not interfere with the instances you are automating inside the form.

MORE INFORMATION
The following file is available for download from the Microsoft Download Center:

"OFramer.exe" Release Date: Mar-23-1999

For additional information about how to download Microsoft Support files, click the article number below to view the article in the Microsoft Knowledge Base:

"Q119591 How to Obtain Microsoft Support Files from Online Services" Microsoft used the most current virus detection software available on the date of posting to scan this file for viruses. Once posted, the file is housed on secure servers that prevent any unauthorized changes to the file.

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 see the following page on the World Wide Web:

"http://www.microsoft.com/partner/referral/" For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

"http://support.microsoft.com/directory/overview.asp"

Sample File Information
The following files are provided with OFramer.exe. All files should be extracted to a folder on your computer. However, Book1.xls, Book2.xls, Doc1.doc and Doc2.doc are not required for Office Framer. These files are simply blank documents included to illustrate the File list box feature of Office Framer:

The main form for Office Framer (Main.frm) is composed primarily of two frame controls. The frame fraExcel is used to house Excel. The frame fraWord is used to house Word. The two frames are positioned on top of each other. Only one frame is made visible at any given time, based on the user's option to display Excel or Word through an option group on the form. The form also contains a File list box control that lists the Excel and Word files on the system. When a user double-clicks a file in the list box, that file is opened and displayed in the appropriate frame through automation. When the user double-clicks another file from the list, the previous file is closed before the next file is opened. Other features of Office Framer demonstrate how to manage the startup, display, and termination of the automation servers.

Additional Features
Office Framer shows how you can check the status of Excel and Word before sending commands to them through automation. The custom function GetServerStatus returns a value that indicates if the server is started, busy, or ready to automate. If GetServerStatus returns "busy" then you can display a message to the user rather than attempt to carry out the automation task. This allows you to avoid run-time errors or confusing "server is busy" messages when your code attempts to automate a busy server. To see an example in Office Framer:


 * 1) Click the "Microsoft Excel" option on the form so that Excel is displayed.
 * 2) Then, click the File Open menu in Excel to display the File Open dialog box.
 * 3) With the Open dialog box still displayed, attempt to open a file by double-clicking a file in the File list box on the form. Note that you see a custom message that tells you the server is busy.

This approach can be very useful when automating an "interactive" automation server because the user could be manipulating the same instance you are trying to automate. Be sure to study the filList_DblClick procedure to see an example of using GetServerStatus.

When the user resizes the form, the two frames resize as well. By sizing the form, the user can control the size of the Excel and Word frames.

The Options menu on the main form allows you to determine whether to view the Caption or StatusBar of the automation server. The advantage of not displaying the Caption and StatusBar is that you have a little more room to display the server in the frame. Also, hiding the caption prevents users from clicking on the caption to change the window state or window position of the automation server.

Instead of using the SetFocus method, Office Framer shows use of the SetForegroundWindow API to set focus to the automation server.

The automation servers are private to Office Framer. For example, if the user double-clicks an .XLS or .DOC file in Windows Explorer, a new instance of Excel or Word is launched. If the user clicks the Start button on the Taskbar to launch Excel or Word, a new instance is launched. When using automation, having a private instance prevents users from inadvertently starting and quitting the instance you are automating.

When the user quits Office Framer, the user is asked to save any unsaved files that are opened. See the QueryUnload event for details. If the user is asked to save a file and clicks Cancel, the shutdown is aborted and Office Framer stays running.

Office Framer also demonstrates how to manipulate CommandBar objects by disabling the File New menu item in both Excel and Word. This change is temporary, only while Office Framer is running. The purpose of disabling the File New menu item is to avoid a refresh problem that can occur when the user drags the File New dialog box of the automation server. When the dialog is moved, Office Framer could "disappear" from the desktop. By disabling the File New menu item you avoid this potential problem. This problem occurs on Windows 95 and 98 but not Windows NT. This problem can be resolved by installing the Microsoft Plus Pack for your version of Windows. This enables the "Show window contents while dragging" feature, which appears to solve this problem. If you wish to enable the File New menu item, comment or remove the CommandBar code in the ShowExcel, QuitExcel, ShowWord, and QuitWord procedures.

When you view Excel and Word through Office Framer, the window size and position of Excel and Word are changed so that they display properly in the frames. When you exit Office Framer, the original window sizes of Excel and Word are restored prior to ending Office Framer.

Limitations
There are some limitations in using this approach versus using the OLE control on a form.

If you are using a database with an OLE field that contains Excel or Word documents, you cannot "bind" to that field in the database as you can when using the OLE control on a form. In this case, the OLE control would be necessary to view the documents in the database. To use Office Framer, you would have to extract the files from the OLE field and write them to disk before viewing them.

Menu Merging does not occur as when in-place activating an object in the OLE control. If you have a menu on the Visual Basic form, it is completely separate from the Excel and Word menus. The Excel and Word menus appear only inside the frames.

The user has full access to the Excel and Word products on the form. If you need to prevent users from accessing certain menus and toolbars, you have to write code to remove or disable them. You can easily manipulate the CommandBars object to manipulate the menus or toolbars. See an example of disabling the File|New menu item in the ShowExcel and ShowWord procedures.

Additional query words:

Keywords : kbfile kbole kbsample kbAutomation kbContainer kbExcel kbInplaceAct kbVBp kbVBp600 kbWebBrowser kbWord kbGrpDSO kbOffice2000

Issue type : kbinfo

Technology : kbWordSearch kbVBSearch kbOfficeSearch kbExcelSearch kbAudDeveloper kbWord97 kbWord97Search kbZNotKeyword6 kbZNotKeyword2 kbOffice2000Search kbVB600Search kbOffice2000 kbOfficeXP kbVB600 kbExcel97Search kbOfficeXPSearch kbZNotKeyword3 kbOfficeXPDevSearch kbOffice2000DevSearch