简体   繁体   English

VBA-在电子邮件正文或主题中查找字符串

[英]VBA - Find string in email body or subject

I am trying to create a simple macro, which reads the active email and checks whether or not a certain string is present. 我正在尝试创建一个简单的宏,该宏读取活动的电子邮件并检查是否存在某个字符串。 Now, the string can have two possible formats, and will only contains digits. 现在,字符串可以有两种可能的格式,并且仅包含数字。

The two formats: 两种格式:

xxx-xxxxxxxx or xxxxxxxxxxx (x will always be a digit) xxx-xxxxxxxxxxxxxxxxxxx (x始终是数字)

I am unsure on how to do this. 我不确定如何执行此操作。 Below I have a macro, which reads the mail - but it can only find a specific string : 在下面,我有一个宏,它读取邮件-但只能找到特定的字符串

Sub AutomateReplyWithSearchString()

    Dim myInspector As Outlook.Inspector
    Dim myObject As Object
    Dim myItem As Outlook.MailItem
    Dim myDoc As Word.Document
    Dim mySelection As Word.Selection
    Dim strItem As String
    Dim strGreeting As String

    Set myInspector = Application.ActiveInspector
    Set myObject = myInspector.CurrentItem

    'The active inspector is displaying a mail item.
    If myObject.MessageClass = "IPM.Note" And myInspector.IsWordMail = True Then
        Set myItem = myInspector.CurrentItem

        'Grab the body of the message using a Word Document object.
        Set myDoc = myInspector.WordEditor
        myDoc.Range.Find.ClearFormatting
        Set mySelection = myDoc.Application.Selection
        With mySelection.Find

            .Text = "xxx-xxxxxxxx"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True

        End With

        If mySelection.Find.Execute = True Then
            strItem = mySelection.Text

            'Mail item is in compose mode in the inspector
            If myItem.Sent = False Then
                strGreeting = "With reference to " + strItem
                myDoc.Range.InsertBefore (strGreeting)
            End If
        Else
            MsgBox "There is no item number in this message."

        End If
    End If
    End Sub

You can use regex pattern: 您可以使用正则表达式模式:

(\d{11}|\d{3}-\d{8})

Try it. 试试吧。

This example is copied from here . 此示例从此处复制。 I have not tested it. 我还没有测试。

Option Explicit

Sub GetValueUsingRegEx()
 ' Set reference to VB Script library
 ' Microsoft VBScript Regular Expressions 5.5

    Dim olMail As Outlook.MailItem
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match

    Set olMail = Application.ActiveExplorer().Selection(1)
   ' Debug.Print olMail.Body

    Set Reg1 = New RegExp

    With Reg1
        .Pattern = "(\d{11}|\d{3}-\d{8})"
        .Global = True
    End With
    If Reg1.test(olMail.body) Then

        Set M1 = Reg1.Execute(olMail.body)
        For Each M In M1
            Debug.Print M.SubMatches(1)

        Next
    End If

End Sub

暂无
暂无

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

相关问题 如何在vba的电子邮件主题行中找到两个单词? - How to find two words in the subject line of email in vba? VBA:查找电子邮件主题和附件名称之间是否存在共同模式 - VBA: Find whether there is common pattern between the email subject and attachment name 使用 Excel Z6E3EC7E6A9F6007B0838FC0EEZ9A 在主题和附件中查找 Outlook Email 关键字 - Find Outlook Email with keywords in Subject & attachment using Excel VBA Outlook VBA 搜索电子邮件主题以获取特定字符串格式并复制到剪贴板 - Outlook VBA to search Email Subject for specific string format and copy to clipboard 如何复制 email 主题的一部分并在基于 QuickParts(Outlook、VBA)的新邮件的 email 正文中使用它 - How to copy a part of an email subject and use it in the email body of a new mail which is based on QuickParts (Outlook, VBA) 通过主题编辑机构转发电子邮件 - Forward email by subject edit body 在 Outlook VBA 中更新电子邮件主题 - Updating email subject in Outlook VBA 将选项卡复制到一个新的未打开的文件中,并在正文和主题中包含文本的电子邮件中发送 - VBA - 宏 - Copy tabs in to a new unopened file and send in email with text in body and subject - VBA - Macro 如果收件人电子邮件相同,则合并电子邮件主题和正文 - merge email subject and body if recipient email is same Outlook按字符串(正文的一部分)在收件箱中找到电子邮件 - Outlook find Email in inbox by string (part of body)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM