Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-08
8
Medium Priority
?
344 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 2000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

963 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