Using PreviewPicture

D

Daniel Smith

Hi Folks,
Is there a way of getting hold of the preview picture of a Visio
Document?

I've looked at one or two ways, there is a PreviewPicture property of
a Visio.document which is a stdole.IPictureDisp type. I didn't seem to
be able to use this though.

A long-winded way would be to convert a VSD file into a VDX file and
then extract the PreviewPicture element. The resulting Hex would then
have to be converted to binary and then saved as a .gif file. But
surely this laborious process would be unnecessary if the
PreviewPicture property could be made to work?

The reason for this question, is that on the project I'm working on we
are displaying pictures of Visio Documents. These previews are
currently .gif files where we have exported the VSD into .gif format
and then constrained the image within a PictureBox on a VB.NET form.
Whilst this works it would be interesting to see if we could use the
PreviewPicture and it should have a nice knock-on effect of being much
smaller in size.

Advice and ideas more than welcome.

Best Regards,

Daniel Smith
 
M

Mai-lan [MS]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top