Solved

Inconsistent print dialog box behavior when try to print Visio diagram using ShellExecuteEx

Posted on 2006-07-08
8
324 Views
Last Modified: 2008-02-26
In the function below, I try to use the API ShellExecuteEx to print documents stored on disk drives.  The documents can be Word, Excel, Visio, etc.  This routine is supposed to automatically print the documents, no matter the format and application, and on my computer it does.  However, on other computers, it may not.  On some of the computers we are using, when a Visio document is opened, a print dialog box displays (with correct printer) and the user is obligated to click on the OK icon to print.  In one case a word document just opened without doing anything else, forcing the user to do the print manually.  And I periodically get printing to a device that is not my default printer.

The only other piece is that, prior to printing, a change the default printer to one that prepares a postscript file.  This is done using the WriteProfileString API, along with Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows") to send message to existing open windows.

The systems involved are either Windows XP Professional or Windows 2000.

The code I use is:
Private objDocument As Object, lpt1 As Printer, dummy
Private gdbCurrent As Database, strTempFile As String
Private Type SHELLEXECUTEINFO
   cbSize As Long
   fMask As Long
   hwnd As Long
   lpVerb As String
   lpFile As String
   lpParameters As String
   lpDirectory As String
   nShow As Long
   hInstApp As Long
   lpIDList As Long
   lpClass As String
   hkeyClass As Long
   dwHotKey As Long
   hIcon As Long
   hProcess As Long
End Type

Declare Function ShellExecuteEx Lib "shell32.dll" Alias _
    "ShellExecuteExA" (shxInfo As SHELLEXECUTEINFO) As Long

Public Function ConvertDocumentToPDF(strFullPath As String) As Long
Dim llngRetCode As Long, ldatStart As Date, lpdfDist As PdfDistiller6
Dim lshexInfo As SHELLEXECUTEINFO, exitCode As Long, hProcess As Long

    AddStack "ConvertDocumentToPDF(" & strFullPath & ")"
    On Error GoTo ErrConvertDocumentToPDF
   
    DeletePrintFiles
    With lshexInfo
        .cbSize = Len(lshexInfo)
        .fMask = SEE_MASK_NOCLOSEPROCESS
        .hwnd = hWndAccessApp
        .lpVerb = "PRINT"
        .lpFile = strFullPath
        .lpParameters = vbNullString
        .lpDirectory = gstrWorkingDir
        .nShow = SW_HIDE
    End With

    'Debug.Print fs.FileExists(gstrPostScriptFile)
    llngRetCode = ShellExecuteEx(lshexInfo)
   
    ConvertDocumentToPDF = PrintToPDF(llngRetCode)

ExitConvertDocumentToPDF:
    SubtractStack
    Exit Function
   
ErrConvertDocumentToPDF:
    ConvertDocumentToPDF = False
    module_errhandler vbOKOnly
    Resume ExitConvertDocumentToPDF
                       
End Function

I would appreciate any help you can provide.

sjl

0
Comment
Question by:sjlevine34
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:Arji
ID: 17077176
To print Office documents, it's much easier to just use the built-in Office automation.  I print Word and Excel documents frequently using Office Automation with no problems.  Since Visio is now a Microsoft product, I would think you could even use Office automation to print those too.  If you are interested, I can post a simple routine that prints Word or Excel documents.  Maybe with a little research there may be a way just as easy as Office automation to print the Visio documents.
0
 
LVL 1

Author Comment

by:sjlevine34
ID: 17077218
The problem is that the documents are potentially in a wide variety of different formats, including jpeg's, etc, although we are trying to standardize on pdf's, which just require copying rather than printing.

Why don't you post the routine and I will see if I can get it to work with Visio.

sjl
0
 
LVL 17

Accepted Solution

by:
Arji earned 500 total points
ID: 17077581
This will print a Word Doc.  When printing multiple documents I just stuff the filenames in an array and cycle through the array with a loop structure.  The same basic code structure will print any Office document.  You just change the Object to whatever app you want to print from.

Dim oApp As Object

        Set oApp = CreateObject("Word.Application")    'Create you app object maybe for you  it would be - CreateObject("Visio.Application")
       
        oApp.Visible = False    'don't want to see Word pop-up
        strFileName = "yourfilename"
       
        oApp.Documents.Open (strFileName)  'open the document invisibly
        oApp.Options.PrintBackground = True   'works when false - a matter of preference
        oApp.ActiveDocument.PrintOut   'prints the document
        oApp.Quit     'closes Word
       
        'Clean up code...
        Set oApp = Nothing
0
 
LVL 17

Expert Comment

by:Arji
ID: 17158526
Thanks.  So did that work for Visio?  It would be helpful to everyone if you posted your solution.  That would be cool if it works with Visio.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 1

Author Comment

by:sjlevine34
ID: 17160992
I didn't try it for visio.  The problem seems to be a lot less of a bother now that the passthru queries we are using are making the report run very fast.
0
 
LVL 1

Author Comment

by:sjlevine34
ID: 17161097
Update:

I just got it to work for visio.  Except for the display of the visio splash screen, the other popup boxes are hidden.

Public Sub printvisiodoc(strFileName As String)

Dim oApp As Object

        Set oApp = CreateObject("Visio.Application")  
       
        oApp.Visible = False    'don't want to see Visio document pop-up
        strFileName = strFileName
       
        oApp.Documents.Open (strFileName)  'open the document invisibly
        oApp.ScreenUpdating = False  
        oApp.ActiveDocument.PrintOut True  'prints the document
        oApp.Quit     'closes Word
       
        'Clean up code...
        Set oApp = Nothing

End Sub

Again, Arji, thanks for your guidance.

sjl
0
 
LVL 1

Author Comment

by:sjlevine34
ID: 17161102
Oh yeah, almost forgot.  The version of Visio I used was Visio 2003.

Sorry for the omission
sjl
0
 
LVL 17

Expert Comment

by:Arji
ID: 17161480
Cool!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now