VBA7中的ShellExecuteEx打印失败,拒绝访问

时间:2013-02-20 02:42:18

标签: windows vba

我们在Word宏中有VBA代码,用于下载一个或多个文档,然后使用Windows函数ShellExecuteEx打印它们。代码在Windows 2000,XP和7(32位和64位)上的Word版本97,2000,2003,2007和2010(32位)中成功运行。

但是对ShellExecuteEx的调用在64位Word 2010和2013中失败。我们已将VBA7(64位)的声明更新为documented on MSDNspecified in the Win32API_PtrSafe file。例如:

#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

用法是这样的:

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

在32位字中它可以工作,但在64位字中,对ShellExecuteEx的调用总是失败,返回5(SE_ERR_ACCESSDENIED)。我尝试了fMask的一系列标记值(包括SEE_MASK_NOASYNC),尝试不为hwnd指定值,为nShow指定不同的值,所有这些都具有相同的失败结果。

更简单的ShellExecute函数适用于32位和64位Word,但它太不灵活了。我们希望使用ShellExecuteEx,因为在打印多个文档时它更好:它使我们能够在发送另一个打印请求之前等待打印应用程序(Word,Adobe Reader等)准备就绪。否则,如果应用程序未准备就绪,则打印请求将失败。 (我尝试在打印请求之间等待几秒钟,但这不可靠。)

为什么ShellExecute打印文件但ShellExecuteEx因拒绝访问而失败?

1 个答案:

答案 0 :(得分:0)

对于64版操作系统,您必须使用LenB而不是Len。 整个答案在这里:http://www.utteraccess.com/forum/office-2010-x64-bit-qu-t1914261.html