繁体   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