簡體   English   中英

在安全模式下打開 Word(從 Outlook VBA)

[英]Opening Word (from Outlook VBA) in Safe Mode

我有一個 Word 文檔作為電子郵件的附件,我需要從中獲取一些數據以傳遞給 URL。 為此,我將附件保存到本地臨時文件,然后打開文檔,然后使用 Word 對象模型從文檔中的表格中提取數據。

我在 VBA 中打開文檔時遇到了一些問題:首先,它非常慢,因為我們有一些公司宏在 Word 打開時加載; 其次,如果 Word 打開時出現任何 ping 消息(例如文檔恢復內容),則代碼只會掛起,而 Word 永遠不會關閉。

所以,我的問題是我可以從 VBA 以安全模式打開 Word,以便只有裸骨文檔可用(因為這就是我所需要的)? 或者,是否有更好的控制 Word 的方法來解決我遇到的問題?

或許用shell在安全模式下打開,說:

"C:\\Program Files\\Microsoft Office\\Office11\\Winword.exe" /a

然后使用 GetObject 獲取實例。

更多細節:

Dim wd As Object
Dim strWord As String, strDoc As String
Dim intSection As Integer
Dim intTries As Integer

    On Error GoTo ErrorHandler

    strWord = "C:\Program Files\Microsoft Office\Office11\WinWord.Exe"
    strDoc = "C:\Docs\ADoc.doc"

    Shell """" & strWord & """ /a", vbMinimizedFocus

    ''Set focus to something other than the word document
    ''See http://support.microsoft.com/kb/238610
    Forms!MyForm.SetFocus

    intSection = 1 ''attempting GetObject...
    Set wd = GetObject(, "Word.Application")
    intSection = 0 ''resume normal error handling

    wd.Documents.Open strDoc

    ''Code here

    Set wd = Nothing

    ''Exit procedure:
    Exit Sub

ErrorHandler:
    If intSection = 1 Then
        intTries = intTries + 1
        If intTries < 20 Then
            Sleep 500 '' wait 1/2 seconds
            Resume ''resume code at the GetObject line
        Else
            MsgBox "GetObject still failing. Process ended.", _
                vbMsgBoxSetForeground
        End If
    Else ''intSection = 0 so use normal error handling:
        MsgBox Error$
    End If

對於睡眠,這需要位於模塊的頂部:

Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

/a 啟動 Word 並防止自動加載加載項和全局模板(包括普通模板)。

/a 開關還會鎖定設置文件; 也就是說,如果使用此開關,則無法讀取或修改設置文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM