Print the Active View

Created:3/27/2001
Description:

This sample shows you how you can print the active view - be it layout or data view.

How to use:
  1. Open a map document in ArcMap.
  2. In the Visual Basic Editor, copy-paste this sample's code into a module.
  3. In either data or layout view, run this procedure to print.
Public Sub PrintActiveView()
  Dim pMxApp As IMxApplication
  Dim pMxDoc As IMxDocument
  Set pMxApp = Application
  Set pMxDoc = ThisDocument
  
  Dim pPrinter As IPrinter
  Dim pPaper As IPaper
  Dim pPageLayout As IPageLayout
  Set pPrinter = pMxApp.Printer
  Set pPrinter.Paper = pMxApp.Paper
  Set pPageLayout = pMxDoc.PageLayout
   
  Dim pActiveView As IActiveView
  Set pActiveView = pMxDoc.ActiveView
  
  Dim deviceframe As tagRECT
  Dim pDeviceFrame As IEnvelope
  
  ' Now set the printer object with the correct properties
  Set pDeviceFrame = New Envelope
  pPageLayout.page.GetDeviceBounds pPrinter, 1, 0, pPrinter.Resolution, pDeviceFrame
  deviceframe.Left = pDeviceFrame.xmin
  deviceframe.top = pDeviceFrame.ymin
  deviceframe.Right = pDeviceFrame.XMax
  deviceframe.bottom = pDeviceFrame.YMax
  
  ' Get the Visible Bounds if we are in Page Layout View
  Dim pVisibleBounds As IEnvelope
  Dim pPageLayoutView As IActiveView
  Set pPageLayoutView = pPageLayout
  If TypeOf pActiveView Is IPageLayout Then
    Set pVisibleBounds = New Envelope
    pPageLayout.page.GetPageBounds pPrinter, 0, 0, pVisibleBounds
  End If
  
  Dim pEmfPrinter As IEmfPrinter
  Set pEmfPrinter = pPrinter
 
  ' Need to offset deviceBounds by xmin and ymin margins only for the EmfPrinter
  If TypeOf pPrinter Is IEmfPrinter Then
    Dim pPrintableBounds As IEnvelope
    Set pPrintableBounds = pPrinter.PrintableBounds
    
    Dim xmin As Double
    Dim ymin As Double
    xmin = pPrintableBounds.xmin
    ymin = pPrintableBounds.ymin
    
    deviceframe.Left = deviceframe.Left - (xmin * pPrinter.Resolution)
    deviceframe.top = deviceframe.top - (ymin * pPrinter.Resolution)
    deviceframe.Right = deviceframe.Right - (xmin * pPrinter.Resolution)
    deviceframe.bottom = deviceframe.bottom - (ymin * pPrinter.Resolution)
  End If
  
  Dim hdc As Long
  hdc = pPrinter.StartPrinting(pDeviceFrame, 0)
  pActiveView.Output hdc, pPrinter.Resolution, deviceframe, pVisibleBounds, Nothing
  
  ' finishing the printing will flush everything out to the print spooler
  pPrinter.FinishPrinting
End Sub