VBA-在电子邮件正文或主题中查找字符串 [英] VBA - Find string in email body or subject

查看:304
本文介绍了VBA-在电子邮件正文或主题中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的宏,该宏读取 active 电子邮件并检查是否存在某个字符串.现在,字符串可以有两种可能的格式,并且仅包含数字.

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.

两种格式:

xxx-xxxxxxxxxxxxxxxxxxx (x始终是数字)

xxx-xxxxxxxx or xxxxxxxxxxx (x will always be a digit)

我不确定如何执行此操作.在下面,我有一个宏,它可以读取邮件-但只能找到 特定字符串 :

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})

尝试一下.

此示例是从此处复制的.我还没有测试.

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

这篇关于VBA-在电子邮件正文或主题中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆