How to Print an Embedded Word Document in Visual Basic

Last reviewed: June 21, 1995
Article ID: Q112196
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0
- Microsoft Word for Windows, version 6.0

SUMMARY

Microsoft Word for Windows version 6.0 disables the ability to use the FilePrint and FilePrintDefault methods while an object is being edited in an OLE container. While the menu options may not be enabled, it is still possible to get around this in code. This article explains how.

MORE INFORMATION

Commands that are part of the workspace are the responsibility of the top container (the Visual Basic application). That is, the application is responsible for the organization of windows, file level operations, and how edits are ultimately saved. The top container must supply a single File menu that contains file level commands such as Open, Close, Save, and Print. If the object is an opened object server application, the commands in its File menu are modified to show containership (Close & Return to <container doc>, Exit & Return to <container doc>).

A well-behaved OLE server will not allow workspace commands to be executed. This is why they are disabled. To work around the problem, edit the object in the server application instead of using in-place editing. In the server workspace, commands are enabled. Therefore, you can edit the object in the server workspace and use OLE Automation to control the server to execute the Workspace commands.

Example Program Using OLE Automation

The following example activates the Word object in the server, and uses OLE Automation to execute the FilePrintDefault method.

NOTE: By default, Word sets background printing On. If Word quits before printing is completed, the print job is aborted. There are two ways to work around this:

  • Define the Word Objects globally. The objects will remain in memory until the container application (Visual Basic) quits. This is the easiest way to do it.

          -or-
    
  • Disable background printing in Word. You can do this by using OLE automation. The command is not available during in-place editing. The following example shows how to do this in code.

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Add a command button (Command1) to Form1.

  3. Add an MSOLE2.VBX control (OLE1) to Form1. When the Insert Object dialog comes up, choose the Create From File option button, and select a Word for Windows document.

  4. Add the following code to the Command1_Click event:

       Sub Command_Click()
          ' Open application in separate application Window:
          ole1.Verb = -2
          ' Activate Object:
          ole1.Action = 7
          Dim WB As object
          ' Alias WordBasic Object:
          Set WB = ole1.Object.application.wordbasic
          ' Disable background printing:
          WB.ToolsOptionsPrint , , , , , , , , , , , 0
          WB.FilePrintDefault  'Print the Word Object.
          ' Hint: it may be necessary to check page layout parameters before
          ' printing. If parameters are outside of the printable region, Word
          ' will display an error message.
       End Sub
    
    

  5. Run the program, and click the Command1 button.


Additional reference words: 3.00
KBCategory: kbole kbprg kbcode
KBSubcategory: IAPOLE PrgCtrlsCus


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: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.