2013-02-20 74 views
3

我們在Word宏中有VBA代碼,用於下載一個或多個文檔,然後使用Windows功能ShellExecuteEx打印它們。代碼在Windows 2000,XP和7(32位和64位)上的Word版本97,2000,2003,2007和2010(32位)上成功運行。ShellExecuteEx在VBA7中打印失敗,訪問被拒絕

但是在64位Word 2010和2013中調用ShellExecuteEx失敗。我們已將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(S​​E_ERR_ACCESSDENIED)。我嘗試了fMask(包括SEE_MASK_NOASYNC)的一系列標誌值,未嘗試指定hwnd的值和nShow的不同值,所有值都具有相同的失敗結果。

更簡單的ShellExecute函數在32位和64位Word中均可用,但它太不靈活。我們希望使用ShellExecuteEx,因爲它在打印多個文檔時更好:它使我們能夠在發送另一個打印請求之前等待打印應用程序(Word,Adobe Reader等)準備就緒。否則,如果應用程序未準備好,打印請求將失敗。 (我試圖在打印請求之間等待幾秒鐘,但這不可靠。)

爲什麼ShellExecute打印文件但ShellExecuteEx失敗並且訪問被拒絕?

+0

同樣的問題。 – outmind 2013-12-12 21:01:00

回答