【问题标题】:Adapted Word VBA code running very slowly in Outlook 2016改编的 Word VBA 代码在 Outlook 2016 中运行非常缓慢
【发布时间】:2018-12-29 12:47:16
【问题描述】:

我有一些 MS Word VBA,当它适应在 Outlook 2016 中运行时,它的运行速度要慢 10 倍。我希望有人能帮助我确定为什么会出现这种情况。 Tomalak 在这个问题中开发了 VBA 代码(删除了 Word 文档中除一个重复的段落之外的所有内容),我对其进行了调整,以便能够在 Outlook 2016 中的消息上运行。我的修改代码如下:

Public Sub STRIPPER_RepeatedTextParasTomalakSimple()
' Add ref to Word Object library and
' MS Scripting Runtime in VBA Editor, Tools, References
Dim objInsp As Outlook.Inspector
Dim objDoc As Word.Document
Dim objWord As Word.Application
Dim Selection As Word.Selection

Set objInsp = Application.ActiveInspector
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection

'------- Inserted Modifed Word VBA below ---------
  Dim p As Word.Paragraph        ' In Word VBA was '....As Paragraph'
  Dim d As New Scripting.Dictionary
  Dim t As Variant
  Dim i As Integer
  Dim StartTime As Single

  StartTime = Timer

  ' collect duplicates
  For Each p In objDoc.Paragraphs    ' In Word VBA was '...In ActiveDocument.Paragraphs
    t = p.Range.Text
    If _
        t <> vbCr Then
        If Not d.Exists(t) Then d.Add Key:=t, Item:=New Scripting.Dictionary
        d(t).Add Key:=d(t).Count + 1, Item:=p
    End If
  Next p

  ' eliminate duplicates
  objWord.ScreenUpdating = False     ' In Word VBA was 'Application.Screenupdating = False'

  For Each t In d
    For i = 1 To d(t).Count - 1
        d(t)(i).Range.Delete         ' This line is the bottleneck
      Next i
  Next t
  objWord.ScreenUpdating = True      ' In Word VBA was 'Application.Screenupdating = True'

MsgBox "This code ran successfully in " & Round(Timer - StartTime, 2) & " seconds", vbInformation
objUndo.EndCustomRecord
'------- End of Modifed Word VBA above ---------


End Sub

希望很明显,表示“插入的修改字 VBA”的部分是原始的 Word VBA 代码,其中 4 个修改由以“在 Word VBA 是.....”开头的注释指示。我还在代码的顶部添加了 4xdims 和 4xsets 以允许代码在 Outlook 中运行。

原代码在 Word 2010 x86 中完美运行,修改后的代码在 Outlook 2016 x86 中完美运行。我不知道为什么在 Outlook 中运行它应该慢得多。 (在 Outlook 中,Word 2016 对象库和 MS Scripting Runtime 都被引用了)

[注意:为避免混淆,我还在EE. 上发布了一个类似标题的问题,这与不同的代码有关。如果任一帖子产生有用的结果,我将在两个场所交叉发布。]

【问题讨论】:

  • 代码无法编译,因为 objWord 是一个对象,而不是包含 Dim p As objWord.Paragraph 中的 Paragraph 类的库(即 Word)。应该是Dim p As Word.Paragraph。我不知道为什么代码运行速度较慢(如果确实如此),但也许 Outlook 中的 Word 检查器较慢?尝试创建“真实”Word 实例复制文本、删除段落并将其复制回 Outlook 时是否更快。
  • 抱歉,我的粘贴错误。我已经编辑并进行了您的更正。
  • 请避开cross postingsexperts-exchange.com/questions/29110029/… 为什么代码不一样?
  • 与在 Word 2010 中运行相比,代码在 Outlook 2016 中以 1/10 的速度运行。根据您的建议,我打开 Word 并创建了一个新 docx,然后将 Outlook 中的一封电子邮件复制到该 Docx 中,然后在 Outlook 中创建了一个新邮件,然后将文本从 Docx 复制到新邮件中,然后运行代码。没有更快。
  • 你还有什么要隐藏的吗?明确表示您使用不同的 Office 版本 (O2016/W2010) 和粗体字符!位数是x86吗?我不同的版本可以有不同的执行时间,但不要问我为什么!如果交叉发布分享链接!

标签: vba outlook ms-word


【解决方案1】:

这是由 Ken White 在 cmets 中回答的,但是我想将其添加为答案,以防再次被问到。肯说

" 在 Word 中执行的代码没有自动化 Word 的开销。Outlook 代码有这个开销,因为它必须将工作传递给 Word 并等待结果。执行自动化的代码永远不会像直接操作的代码。与您向其他人解释您想要什么工具并等待他们将其交给您相比,您自己从工作台上拿起工具所花费的时间更少。”

【讨论】:

    猜你喜欢
    • 2014-03-19
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    相关资源
    最近更新 更多