Solved

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

Posted on 2006-07-08
8
326 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

864 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

23 Experts available now in Live!

Get 1:1 Help Now