Hi, there: Here's a code sample in a VB form that does print preview. I've
copied it with all the form data so you can get a sense of what's on it.
Sorry that the formatting is a little messed up.
VERSION 5.00
Begin VB.Form frmPrintPreview
Caption = "Print Preview"
ClientHeight = 4185
ClientLeft = 60
ClientTop = 360
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 4185
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.PictureBox pctPreview
BorderStyle = 0 'None
Height = 3360
Left = 105
ScaleHeight = 3360
ScaleWidth = 4395
TabIndex = 2
Top = 135
Width = 4395
Begin VB.Label lblNoMetafile
Alignment = 2 'Center
Caption = "No Preview is Available"
BeginProperty Font
Name = "MS Sans Serif"
Size = 18
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1065
Left = 255
TabIndex = 3
Top = 885
Width = 3870
End
End
Begin VB.CommandButton cmdPrint
Caption = "Print"
Height = 420
Left = 1965
TabIndex = 1
Top = 3615
Width = 1215
End
Begin VB.CommandButton cmdCancel
Cancel = -1 'True
Caption = "Cancel"
Height = 420
Left = 3330
TabIndex = 0
Top = 3600
Width = 1215
End
End
Attribute VB_Name = "frmPrintPreview"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' frmPrintPreview / PrintPreview.bas
' Copyright (c) Microsoft Corporation. All rights reserved.
'<ignore>
Option Explicit
'</ignore>
' This file contains code that creates a print preview form from
' a Visio drawing.
' The document and printer values are saved to use if the user clicks on the
' Print button.
Dim vsoTargetDocument As Visio.Document
Dim strSelectedPrinter As String
' The PlayMetaFile Windows API function is used to place the metafile
' of the Visio page into the picture control on the form.
Private Declare Function PlayMetaFile Lib "gdi32" (ByVal hDC As Long, _
ByVal hMF As Long)
Private Sub cmdCancel_Click()
'
' cmdCancel_Click
'
' Abstract - This procedure is run when the user clicks on the Cancel
' button. The form is hidden.
'
On Error GoTo cmdCancel_Click_Err
Me.Hide
Exit Sub
cmdCancel_Click_Err:
' Display the error
If Not (vsoTargetDocument Is Nothing) Then
If (vsoTargetDocument.Application.AlertResponse = 0) Then
MsgBox Err.Description
Else
Debug.Print Err.Description
End If
Else
Debug.Print Err.Description
End If
End Sub
Private Sub cmdPrint_Click()
'
' cmdPrint_Click
'
' Abstract - This procedure is called when the user clicks on the Print
' button. The document is printed using the Visio Print method.
'
Dim strCurrentActivePrinter As String
On Error GoTo cmdPrint_Click_Err
' Set up the desired printer. Save the current printer
' so that it can be restored when this print process is
' done.
strCurrentActivePrinter = _
vsoTargetDocument.Application.ActivePrinter
If strCurrentActivePrinter <> strSelectedPrinter Then
vsoTargetDocument.Application.ActivePrinter = _
strSelectedPrinter
End If
' Use the Visio print command to print the document.
vsoTargetDocument.Print
' Restore the original active printer.
If strCurrentActivePrinter <> strSelectedPrinter Then
vsoTargetDocument.Application.ActivePrinter = _
strCurrentActivePrinter
End If
Exit Sub
cmdPrint_Click_Err:
' Display the error
If Not (vsoTargetDocument Is Nothing) Then
If (vsoTargetDocument.Application.AlertResponse = 0) Then
MsgBox Err.Description
Else
Debug.Print Err.Description
End If
Else
Debug.Print Err.Description
End If
End Sub
Public Sub PreviewDrawing(ByVal vsoSubjectDocument As Visio.Document, _
Optional ByVal strPrinter As String = "")
'
' PreviewDrawing
'
' Abstract - This procedure takes a Visio document and, optionally, a
' printer. The document's current page's picture is loaded into the
' form control, and the document and printer information is saved to
' use when printing.
' The dialog must be shown with a call to ShowDialog() after this procedure
' is called to display the dialog to the user.
'
' The Visio Picture property is not available from a separate executable.
' If a COM error is raised, a message will be shown on the dialog,
' but the user can still hit the Print button.
'
' Parameters:
' vsoSubjectDocument Visio document to preview and print
' strPrinter Name of the printer to print to (If a value isn't
' specified, the current Visio active printer is
' used.)
'
Dim vsoPage As Visio.Page
Dim hdlMetafile As Long
On Error GoTo PreviewDrawing_Err
' Save the document and the printer for use if the Print
' button is clicked.
Set vsoTargetDocument = vsoSubjectDocument
If Len(strPrinter) > 0 Then
strSelectedPrinter = strPrinter
Else
strSelectedPrinter = _
vsoSubjectDocument.Application.ActivePrinter
End If
' Get the metafile from the drawing.
' Handle the error in a special way, since if the picture isn't
' available it's most likely because Visio isn't running in the
' same executable. Just show the 'not available' message.
Set vsoPage = vsoSubjectDocument.Pages(1)
On Error Resume Next
hdlMetafile = vsoPage.Picture.Handle
If Err.Number <> 0 Then
lblNoMetafile.Visible = True
Else
lblNoMetafile.Visible = False
End If
On Error GoTo PreviewDrawing_Err
' If the metafile was created correctly, load it into the control.
If lblNoMetafile.Visible = False Then
PlayMetaFile pctPreview.hDC, hdlMetafile
End If
Exit Sub
PreviewDrawing_Err:
' Display the error
If Not (vsoSubjectDocument Is Nothing) Then
If (vsoSubjectDocument.Application.AlertResponse = 0) Then
MsgBox Err.Description
Else
Debug.Print Err.Description
End If
Else
Debug.Print Err.Description
End If
End Sub
Thanks,
Mai-lan