Solved

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

Posted on 2006-07-08
8
329 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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
 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

776 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