简体   繁体   English

在安全模式下打开 Word(从 Outlook VBA)

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

I have a Word document as an attachment to an email that I need to get some data out of to pass to a URL.我有一个 Word 文档作为电子邮件的附件,我需要从中获取一些数据以传递给 URL。 To do this I'm saving the attachment to the local temp file, then opening the document, and then using the Word object model to pull data out of tables in the document.为此,我将附件保存到本地临时文件,然后打开文档,然后使用 Word 对象模型从文档中的表格中提取数据。

I'm having a couple of problems when opening the document in VBA: firstly it's very slow because we have some corporate macro stuff that loads when Word opens;我在 VBA 中打开文档时遇到了一些问题:首先,它非常慢,因为我们有一些公司宏在 Word 打开时加载; and secondly if there are any messages that ping up when Word opens (such as document recovery stuff), the code just hangs and Word is never closed.其次,如果 Word 打开时出现任何 ping 消息(例如文档恢复内容),则代码只会挂起,而 Word 永远不会关闭。

So, my question is can I open Word in safe mode from VBA so that nothing but the bare bones document is available (because that's all I need)?所以,我的问题是我可以从 VBA 以安全模式打开 Word,以便只有裸骨文档可用(因为这就是我所需要的)? Or, is there a better way of controlling Word that gets around the issues I'm having?或者,是否有更好的控制 Word 的方法来解决我遇到的问题?

Perhaps use shell to open in safe mode, say:或许用shell在安全模式下打开,说:

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

Then use GetObject to get the instance.然后使用 GetObject 获取实例。

More details:更多细节:

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

For Sleep, this needs to go at the top of the module:对于睡眠,这需要位于模块的顶部:

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

/a Starts Word and prevents add-ins and global templates (including the Normal template) from being loaded automatically. /a 启动 Word 并防止自动加载加载项和全局模板(包括普通模板)。

The /a switch also locks the setting files; /a 开关还会锁定设置文件; that is, the setting files cannot be read or modified if you use this switch.也就是说,如果使用此开关,则无法读取或修改设置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM