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

发布于 2024-09-11 19:09:20 字数 315 浏览 4 评论 0原文

我有一个 Word 文档作为电子邮件的附件,我需要从中获取一些数据以传递到 URL。为此,我将附件保存到本地临时文件,然后打开文档,然后使用 Word 对象模型从文档中的表中提取数据。

在 VBA 中打开文档时,我遇到了一些问题:首先,它非常慢,因为我们有一些公司宏内容在 Word 打开时加载;其次,如果在 Word 打开时出现任何消​​息(例如文档恢复内容),代码就会挂起并且 Word 永远不会关闭。

所以,我的问题是我可以从 VBA 以安全模式打开 Word,这样除了基本文档之外什么都可用(因为这就是我所需要的)?或者,是否有更好的方法来控制 Word 来解决我遇到的问题?

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. 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.

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; 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.

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)? Or, is there a better way of controlling Word that gets around the issues I'm having?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-09-18 19:09:20

也许使用 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 并防止自动加载加载项和全局模板(包括 Normal 模板)。

/a 开关还锁定设置文件;也就是说,如果使用此开关,则无法读取或修改设置文件。

Perhaps use shell to open in safe mode, say:

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

Then use GetObject to get the instance.

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.

The /a switch also locks the setting files; that is, the setting files cannot be read or modified if you use this switch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文