Solved

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

Posted on 2006-07-08
8
333 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
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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 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