首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShellExecuteEx在VBA7中打印失败,访问被拒绝

ShellExecuteEx在VBA7中打印失败,访问被拒绝
EN

Stack Overflow用户
提问于 2013-02-20 10:42:18
回答 1查看 1.2K关注 0票数 3

我们在Word宏中有VBA代码,用于下载一个或多个文档,然后使用窗口函数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。例如:

代码语言:javascript
复制
#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

用法如下:

代码语言:javascript
复制
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位字中都有效,但它太不灵活了。我们想要使用ShellExecuteEx,因为它在打印多个文档时更好:它使我们能够等待打印应用程序(Word、Adobe Reader等)。在发送另一个打印请求之前准备就绪。否则,如果应用程序未就绪,打印请求将失败。(我尝试在打印请求之间简单地等待几秒钟,但这并不可靠。)

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

EN

回答 1

Stack Overflow用户

发布于 2013-12-13 06:09:16

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14971428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档