Tuesday, November 24, 2009

Programmatically setting Internet Explorer print settings for Header, Footer and Margins

The following ampersand values can be used in custom headers and footers.

  • && = ampersand
  • &b = right-justify
  • &b&b = Text following the first "&b" in center alignment; text following the second "&b" in right-justified alignment
  • &d = date (user short format)
  • &D = date (user long format)
  • &t = time (user preference)
  • &T = time (military format)
  • &p = page
  • &P = number of pages
  • &u = URL (or file path)
  • &w = (window) title tag

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

  • header
  • footer
  • margin_left
  • margin_right
  • margin_top
  • margin_bottom
  • shrink_to_fit ("yes" or "no")
  • Print_Background ("yes" or "no")
  • font ("font-family: KabaleMedium; font-size: 12pt; color: rgb(0,0,0); font-weight: bold;")
  • (unverified) printer
  • (unverified) orientation ("1" =Portrait, "2" = Landscape)
  • (unverified) duplex ("1" = Off, "2" = On)

 

   1:  Imports Microsoft.Win32
   2:   
   3:  Public Class Form1
   4:   
   5:      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   6:          AxWebBrowser1.Navigate("http://www.bing.com")
   7:      End Sub
   8:   
   9:      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10:          Dim IERegKey As RegistryKey
  11:          Dim header, footer As String
  12:   
  13:          IERegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", True)
  14:          footer = IERegKey.GetValue("footer", 0)
  15:          header = IERegKey.GetValue("header", 0)
  16:   
  17:          IERegKey.SetValue("footer", "")
  18:          IERegKey.SetValue("header", "")
  19:   
  20:          AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT)
  21:   
  22:          ' Reset the values after printing is completed
  23:          IERegKey.SetValue("footer", footer)
  24:          IERegKey.SetValue("header", header)
  25:      End Sub
  26:   
  27:  End Class

 

From http://support.microsoft.com/kb/283797/en-us
PRB: Programmatically Issuing Print Command May Not Print to Default Printer

When you select a printer in Internet Explorer (on the File menu, click Page Setup, click Printer, and choose a printer that is not the system default), your printer selection is stored in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\printer

After you make this selection, Internet Explorer continues to use the printer value that is stored in the registry.

 

Print Templates - http://msdn.microsoft.com/en-us/library/aa753279(VS.85).aspx

1 comment:

Dan Fields said...

Hey, this is super old, and I'm not sure if you're even keeping up with this any longer, but you really helped me out today. Thanks for the post.