【问题标题】:Find and put in italic a list of words (that are listed in a.txt file) in all my Outlook message在我的所有 Outlook 邮件中查找并以斜体形式输入单词列表(在 a.txt 文件中列出)
【发布时间】:2021-02-21 12:10:46
【问题描述】:

我正在尝试在我的所有 Outlook 邮件中查找并以斜体形式放入一个单词列表(在 .txt 文件中列出)。

我在 Outlook 中尝试了一个 Word VBA 宏(找到它here)。

它适用于 Word。

在 Outlook 中我得到

运行时错误 424,需要对象。

Sub ChangeWordColorsFile()
    Dim sFile As String
    Dim sTemp As String
    
    sFile = "C:\Users\me\Desktop\List words.txt"
    
    Open sFile For Input As 1
    
    While Not EOF(1)
        Line Input #1, sTemp
        sTemp = Trim(sTemp)
    
        If sTemp > "" Then
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            Selection.Find.Replacement.Font.Italic = True
            With Selection.Find
                .Text = sTemp
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = True
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
        End If
    Wend
    Close #1
End Sub

【问题讨论】:

  • Selection 在 Outlook 中不存在。你想做什么?
  • 我正在尝试在我的所有 Outlook 邮件中自动查找并以斜体形式放入一个单词列表(在 .txt 文件中列出)
  • 您是否拥有(或打算拥有)一个开放的 Word 应用程序?我不知道Outlook object model 是否有这样的查找/替换功能。 OTOH,如果您可以在 UI 中执行此操作,您可能可以从 VBA 执行此操作。你能在 Outlook UI 中做你想做的事吗?

标签: vba outlook ms-word


【解决方案1】:

要在 Outlook 上使用它,请参阅下面的示例

Option Explicit
Public Sub Example()
    Dim sFile As String
    Dim sTemp As String
    
    Dim Inspector As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim Selection As Word.Selection

    Set Inspector = Application.ActiveInspector()
    Set wdDoc = Inspector.WordEditor
    Set Selection = wdDoc.Application.Selection

    sFile = "D:\Temp\words.txt"

    Open sFile For Input As 1

    While Not EOF(1)
        Line Input #1, sTemp
        sTemp = Trim(sTemp)

        If sTemp > "" Then
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            Selection.Find.Replacement.Font.Italic = True
            With Selection.Find
                .Text = sTemp
                .Replacement.Text = ""
                .Forward = True
                .wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = True
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
        End If
    Wend
    
    Close #1
End Sub

Make sure to Reference to Microsoft Word xx.x Object Library

Application.ActiveInspector()上测试的代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 2011-07-08
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多