Printing Landscape with VB

Posted on 1998-12-01
Last Modified: 2008-01-16
Can anyone tell me if there is a way to force a print to come out in Landscape instead of Portrait.

Question by:BLumas
LVL 13

Accepted Solution

Mirkwood earned 30 total points
ID: 1447600
VB3 How to Set Landscape or Portrait for Printer from VB App

Last reviewed: January 9, 1997
Article ID: Q80185 The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

- Microsoft Visual Basic programming system for Windows, version 1.0


Some printers support changing the orientation of the paper output to landscape. With the Windows API Escape() function, you can change the settings of the printer to either landscape or portrait. In addition, if you have one of the following products, you can use the Common Dialog box to allow users to set the mode inside a Visual Basic Application:

•Visual Basic version 1.0 Professional Toolkit •Professional Edition of Visual Basic version 2.0 •Standard or Professional Edition of Visual Basic version 3.0

Below is an example showing how to invoke the Windows API Escape() function from Microsoft Visual Basic.

NOTE: The Windows API Escape() function is provided in Windows versions 3.0 and 3.1 for backward compatibility with earlier versions of Microsoft Windows. Applications are supposed to use the GDI DeviceCapabilities() and ExtDeviceMode() functions instead of the Escape() function, but neither DeviceCapabilities() nor ExtDeviceMode() can be called directly from Visual Basic. This is because they are exported by the printer driver, not by the Windows GDI. The only way to use ExtDeviceMode() or DeviceCapabilities() in Visual Basic is to create a DLL and call them from there.


Normally, output for the printer is in portrait mode, where output is printed horizontally across the narrower dimension of a paper. In landscape mode, the output is printed horizontally across the longer dimension of the paper.

You can use the Escape() function to change the orientation of the printer by passing GETSETPAPERORIENT as an argument. When you initially print text to the printer, Visual Basic will use the currently selected orientation. Sending the Escape() function will not take effect until you perform a Printer.EndDoc. After you perform a Printer.EndDoc, output will print in the orientation that you have selected.

To determine if your printer supports landscape mode, do the following:

1.From the Windows Program Manager, run Control Panel.

2.From the Control Panel, select the Printers icon.

3.From the Printers dialog box, choose the Configure button.

4.The Configure dialog box will contain an option for landscape orientation if landscape is supported on your printer.

How to Check the Current Orientation of the Printer

To check the current orientation of the printer, use the following code:

' Enter the following Declare statement as one, single line:
Declare Function Escape% Lib "GDI" (ByVal hDC%, ByVal nEsc%, ByVal nLen%,
   lpData As Any, lpOut As Any)

Sub Command1_Click ()
   Const PORTRAIT = 1
   Const LANDSCAPE = 2

   Dim Orient As OrientStructure
   Printer.Print ""
   Orient.Orientation = LANDSCAPE
   x% = Escape(Printer.hDC, GETSETPAPERORIENT, Len(Orient), "", Null)
   Print x%
End Sub

How to Change the Printer Orientation to Landscape

The following example below demonstrates how to change the printer orientation to landscape. Please note that your printer must support landscape mode for these commands to have any effect.

1.Start a new project in Visual Basic (ALT, F, N). Form1 is created by default.

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

3.Add the following code to the global module:

Type OrientStructure

      Orientation As Long
      Pad As String * 16

End Type

   ' Enter the following Declare statement on one, single line:
   Declare Function Escape% Lib "GDI" (ByVal hDc%, ByVal nEsc%,
               ByVal nLen%, lpData As OrientStructure, lpOut As Any)

4.Add the following code to the Command1_Click event procedure of the Command1 button:

   Sub Command1_Click ()
      Const PORTRAIT = 1
      Const LANDSCAPE = 2

      Dim Orient As OrientStructure

      '* Start the printer
      Printer.Print ""

      '* Specify the orientation
      Orient.Orientation = LANDSCAPE

      '* Send escape sequence to change orientation
      x% = Escape(Printer.hDC, GETSETPAPERORIENT,
                Len(Orient), Orient, NULL)
      '* The EndDoc will now re-initialize the printer

      Printer.Print "Should print in landscape mode"
   End Sub

Expert Comment

ID: 1447601
There is a real problem using the common dialog for printer, I'm looking for a way to print using API
 functionsin VB5 for NT & WIN95.


Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question