我们在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 MSDN和specified 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位字中都有效,但它太不灵活了。我们想要使用ShellExecuteEx,因为它在打印多个文档时更好:它使我们能够等待打印应用程序(Word、Adobe Reader等)。在发送另一个打印请求之前准备就绪。否则,如果应用程序未就绪,打印请求将失败。(我尝试在打印请求之间简单地等待几秒钟,但这并不可靠。)
为什么ShellExecute打印文件但ShellExecuteEx失败并拒绝访问?
发布于 2013-12-13 06:09:16
对于64版本的操作系统,您必须使用LenB而不是Len。完整的答案在这里:http://www.utteraccess.com/forum/office-2010-x64-bit-qu-t1914261.html
https://stackoverflow.com/questions/14971428
复制相似问题