Solved

How do I print in landscape?

Posted on 2004-09-19
9
2,835 Views
Last Modified: 2013-12-26
I have a file "c:\abc.htm".

From my VB app I want to let the user push a button and print that file to a printer in landscape orientation:

I currently use this line when user clicks the command button:

 WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0
   
0
Comment
Question by:shaolinfunk
  • 4
  • 4
9 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 12095786
you could have a look at
title : Printing Landscape w/web browser control
source : http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=e87oNziFBHA.1420%40tkmsftngp04

HTHAB

shaolinfunk hehe, nice handle ;)
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 12096424
thanks for the URL.  what is regread and regwrite?
why is everything after "Dim retVal" highlighted in red in my VB editor?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 12096431
what is HTHAB?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:shaolinfunk
ID: 12096436
is it object.regread and object.regwrite? what is the object?
0
 
LVL 44

Expert Comment

by:bruintje
ID: 12096439
because of the line breaks, here is an edited version

Option Explicit

'------------------
Public Function setOrientation(ByVal theOrientation As String) As String
    ' changes the printers orientation and returns the old value.
    Dim retVal
    setOrientation = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "orientation")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "orientation", theOrientation)
End Function
'------------------

Private Sub test()

'Example of use
Dim originalOrientation As String

'Set the oreintation to landscape
originalOrientation = modPrinter.setOrientation("2")

'Print the document
webbrowser1.SetFocus
webbrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0

'Change the orientation back to the original orientation
originalOrientation = modPrinter.setOrientation(printDefaultOrientation)

End Sub

' Here are some other functions that may be of use to you: HTH Steven Allen  (dybrn)
'-----------------
'Found at http://support.microsoft.com/support/kb/articles/Q236/7/77.ASP

'For Page Margins, Microsoft Internet Explorer first
'tries to get the values from this registry key:
'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

'If there is no such a key, Internet Explorer create
'this key by copying the values from the following:
'HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\PageSetup

'If there is no such key, default values are provided.

'For the Header and Footer, the values are picked up from the following:
'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

'If there is no such key, default values are provided.
'The defaults are
    ' Margins:  0.75
    ' Header:   &w&bPage &p of &P
    ' Footer:   &u&b&d

'For the Internet Explorer default printer, default values are provided
from:
'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\printer

Public Type printerMargins
    margin_left As String
    margin_right As String
    margin_top As String
    margin_bottom As String
End Type

Public Function setOrientation(ByVal theOrientation As String) As String
    ' changes the printers orientation and returns the old value.
    Dim retVal
    setOrientation = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "orientation")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "orientation", theOrientation)
End Function

Public Function setHeader(ByVal theHeader As String) As String
    ' changes the header and returns the old value.
    Dim retVal
    setHeader = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "header")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "header", theHeader)
End Function

Public Function setFooter(ByVal theFooter As String) As String
    ' changes the Footer and returns the old value.
    Dim retVal
    setFooter = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "Footer")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "Footer", theFooter)
End Function

Public Function SetDuplex(ByVal theValue As String) As String
    ' Turns on or off the duplexer depending on "theValue".
    Dim retVal
    Dim newValue As String

    If theValue = "2" Then
        newValue = "2" 'ON
    Else
        newValue = "1" 'OFF
    End If
    SetDuplex = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "duplex")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "duplex", newValue)
End Function

Public Function SetMargins(thePrinterMargins As printerMargins) As printerMargins
    Dim retVal

    SetMargins.margin_left = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_left")
    SetMargins.margin_right = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_right")
    SetMargins.margin_top = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_top")
    SetMargins.margin_bottom = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom")

    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_left", thePrinterMargins.margin_left)
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_right", thePrinterMargins.margin_right)
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_top", thePrinterMargins.margin_top)
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom", thePrinterMargins.margin_bottom)

End Function
0
 
LVL 44

Accepted Solution

by:
bruintje earned 50 total points
ID: 12096472
ok this should help a bit

first download the registry unit from
http://www.buygold.net/v01n05/v01n05.html

add the treg *.bas to your project

' ---------------------
' then this part goes into a new module
' ---------------------
Public Type printerMargins
    margin_left As String
    margin_right As String
    margin_top As String
    margin_bottom As String
End Type

Public Function setOrientation(ByVal theOrientation As String) As String
    ' changes the printers orientation and returns the old value.
    Dim retVal
    setOrientation = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "orientation")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "orientation", theOrientation)
End Function

Public Function setHeader(ByVal theHeader As String) As String
    ' changes the header and returns the old value.
    Dim retVal
    setHeader = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "header")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "header", theHeader)
End Function

Public Function setFooter(ByVal theFooter As String) As String
    ' changes the Footer and returns the old value.
    Dim retVal
    setFooter = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "Footer")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "Footer", theFooter)
End Function

Public Function SetDuplex(ByVal theValue As String) As String
    ' Turns on or off the duplexer depending on "theValue".
    Dim retVal
    Dim newValue As String

    If theValue = "2" Then
        newValue = "2" 'ON
    Else
        newValue = "1" 'OFF
    End If
    SetDuplex = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "duplex")
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "duplex", newValue)
End Function

Public Function SetMargins(thePrinterMargins As printerMargins) As printerMargins
    Dim retVal

    SetMargins.margin_left = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_left")
    SetMargins.margin_right = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_right")
    SetMargins.margin_top = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_top")
    SetMargins.margin_bottom = RegRead("Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom")

    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_left", thePrinterMargins.margin_left)
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_right", thePrinterMargins.margin_right)
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_top", thePrinterMargins.margin_top)
    retVal = RegWrite("Software\Microsoft\Internet Explorer\PageSetup", "margin_bottom", thePrinterMargins.margin_bottom)

End Function

Public Sub test()

'Example of use
Dim originalOrientation As String
Dim modPrinter As Printer

'Set the oreintation to landscape
originalOrientation = setOrientation("2")

'Print the document
webbrowser1.SetFocus
webbrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0

'Change the orientation back to the original orientation
originalOrientation = setOrientation(printDefaultOrientation)

End Sub
' ---------------------

' ---------------------
' this goes in the click
' ---------------------
Private Sub Command1_Click()
  Call test
End Sub
' ---------------------

away for a few hours now
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 12098971
Is there a way to print Landscape without editing the Registry?
0
 
LVL 44

Expert Comment

by:bruintje
ID: 12130412
no, looked for that at the start
0
 

Expert Comment

by:Magnum13Yancey
ID: 22140561
The method above works farely well, but has anyone had issues when setting orientation? I'm on vista with IE7 and when I change the orientation value to 2 instead of 1 my document still prints in portrait. Does IE7 look in a different spot for the orientation?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

680 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