You are currently offline, waiting for your internet to reconnect

How to make Word print duplex by using OLE automation from Visual FoxPro

This article was previously published under Q194306
This article describes how to use OLE automation from Visual FoxPro toMicrosoft Word for Windows 97 to print a document duplex.
Since the dialog box that sets duplex printing is part of the printersetup, there is not a direct command that you can issue that causes adocument to print duplex. However, there are three ways that do workallowing you to print documents duplex.

The printer used in this test is a Hewlett-Packard LaserJet printer, whichsupports duplex printing.

Method 1

Use the PRINT field in Word to send the printer escape code that puts theprinter in duplex mode. The following code demonstrates this:
   oWord=CREATEOBJECT("Word.Application")   WITH oWord      .Visible=.T.      .Documents.Add      .Activewindow.View.ShowFieldCodes=0   && Don't view field codes.      * Insert PRINT field & Hewlett-Packard escape code for duplex, long-      * edge binding.      * It is an ampersand, lower-case L, numeric 1, and upper-case S.      .Selection.Fields.Add(.Selection.Range,-1,"PRINT 27 &l1S")      .Selection.Moveright      .Selection.Insertafter("Page 1")      .Selection.Moveright      odlog=oWord.Dialogs(159) && Insert page break w/ Dialogs collection.      odlog.Execute      .Selection.Insertafter("Page 2")      .Selection.Moveright      .Printout(0)                   && 0 turns off background printing.      .Application.Quit(0)           && Quit, don't save & don't prompt.   ENDWITH				
This code does not work if the Word document contains mixed orientation,that is portrait and landscape sections.

Note This code only works with the Service Release-1 (SR-1) for Word 97.Prior versions of Word 97 do not duplex print using this code.

For the Word document to print correctly, it should not contain headers orfooters on every page. However, it should print duplex if the document issetup to have a different first page header or footer only and the PRINTfield is inserted on the first line of the header followed by a carriagereturn.

Method 2

Use the SendKeys command to send keystrokes to Word, which simulates usingkeystrokes to open the Print dialog box.
   oWord=CREATEOBJECT("Word.Application")   WITH oWord      .Visible=.T.      .Documents.Add      * This code creates a two page document to print.      .Selection.Insertafter("Page 1 - With SendKeys")      .Selection.Moveright      odlog=oWord.Dialogs(159) && Insert page break w/ Dialogs collection.      odlog.Execute      .Selection.Insertafter("Page 2 - With SendKeys")      .Selection.Moveright      * End of document creation.      .Application.Activate      .Wordbasic.Sendkeys("%fp%p%d{enter}{tab}{enter}", -1)      =INKEY(2)   && May need to wait couple seconds before quitting.      .Application.Quit(0)           && Quit, don't save & don't prompt.   ENDWITH				
In the Sendkeys command, the % sign represents using the ALT key to accessWindows menus. The INKEY() command allows Word to process the keystrokesbefore Visual FoxPro for Windows processes the Quit. This may need to beadjusted up, down, or removed depending on the application.

To use Sendkeys, Word must be visible, not minimized, since the keystrokesmust go to an active window. You can move Word off screen by using theAppMove command, so that Word does not show, but the dialog box stilldisplays. Also, using Sendkeys can be disconcerting because the menus anddialog boxes open and process as if you were in Word and pressed the ALT+Fkeys to open the File menu.

Method 3

Create a Word macro that issues the Sendkeys. If the macro is namedSendKey, then run the macro from Visual FoxPro using the following command:
     oWord=CREATEOBJECT("Word.Application")       WITH oWord       .Visible=.T.       .Documents.Add       .Application.Activate       .Run("SendKey")       .Application.Quit(0)           && Quit, don't save & don't prompt.    ENDWITH				
The minimum code for the Word macro to send the keys for duplex printingfollows:
   Sub SendKey()      SendKeys "%fp%p%d{enter}{tab}{enter}"   End Sub				
Note Please see the Note in Method 2 for problems you may encounter usingSendKeys.
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
135569 PCL escape codes to use with the PRINT field
For additional information about how to obtain Word for Windows 97 Service Release 1, click the following article number to view the article in the Microsoft Knowledge Base:
172475 How to obtain and install MS Office 97 SR-1
Microsoft Word Visual Basic Help

(c) Microsoft Corporation 1998. All Rights Reserved. Contributions by DeanChristopher,Microsoft Corporation.
kbVFp600 kbole kbInterOp kbvfp500a

Article ID: 194306 - Last Review: 01/27/2005 08:34:21 - Revision: 2.2

  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • kbcode kbhowto KB194306