ShellExecuteEx print in VBA7 fails with access denied

We have VBA code in a Word macro that is used to load one or more documents and then print them using a Windows function.The ShellExecuteEx

code works successfully on Word 97, 2000, 2003, 2007 and 2010 (32 bit) versions on Windows 2000 , XP and 7 (32-bit and 64-bit).

But the call ShellExecuteEx

fails in 64-bit Word 2010 and 2013. We've updated the declarations for VBA7 (64-bit) as documented on MSDN and listed in the Win32API_PtrSafe . For example:

#If VBA7 Then
Type SHELLEXECUTEINFO
    cbSize As Long
    fMask As Long
    hwnd As LongPtr
    lpVerb As String
    lpFile As String
    lpParameters As String
    lpDirectory As String
    nShow As Long
    hInstApp As LongPtr
    lpIDList As LongPtr
    lpClass As String
    hkeyClass As LongPtr
    dwHotKey As Long
    hIcon As LongPtr
    hProcess As LongPtr
End Type
Declare PtrSafe Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" _
    (sei As SHELLEXECUTEINFO) As Boolean
#End If

      

Usage looks like this:

Dim bReturn As Boolean
Dim sei As SHELLEXECUTEINFO

With sei
    .cbSize = Len(sei)                  ' size of the object
    .fMask = SEE_MASK_NOCLOSEPROCESS    ' indicate that we want a hProcess back
    .hwnd = GetDesktopWindow()          ' the window we are calling from
    .lpVerb = "print"                   ' print the file
    .lpFile = lpFile                    ' the file we want to print
    .lpParameters = vbNullString        ' no parameters because its a file
    .lpDirectory = vbNullString         ' use the current dir as working dir
    .nShow = SW_HIDE                    ' state of the window to open
End With

bReturn = ShellExecuteEx(sei)

If bReturn Then
    WaitForSingleObject sei.hProcess, 5000
    CloseHandle sei.hProcess
    DoEvents
Else
    MsgBox "ShellExecuteEx failed with code: " & Err.LastDllError
End If

      

On 32 bit Word it works, but on 64 bit Word the call ShellExecuteEx

always fails, returning 5 (SE_ERR_ACCESSDENIED). I've tried a number of flag values ​​for fMask

(including SEE_MASK_NOASYNC), didn't try to specify a value for hwnd

and different values ​​for nShow

, all with the same unfortunate result.

The simpler function ShellExecute

works in both 32-bit and 64-bit Word, but it's too inflexible. We want to use ShellExecuteEx

because it is better to print multiple documents: this gives us the ability to wait for the print application (Word, Adobe Reader, etc.) to be ready before sending another print request. Otherwise, the print request will fail if the application is not ready. (I've tried just waiting a few seconds between print requests, but this is not reliable.)

Why is ShellExecute

printing files but ShellExecuteEx

access denied?

+3


source to share


1 answer


You need to use LenB instead of Len for 64-bit OS. The whole answer is here: http://www.utteraccess.com/forum/office-2010-x64-bit-qu-t1914261.html



0


source







All Articles