VBA-在电子邮件正文或主题中查找字符串
我正在尝试创建一个简单的宏,它读取活动电子邮件并检查是否存在某个字符串。现在,字符串可以有两种可能的格式,并且只包含数字 两种格式:VBA-在电子邮件正文或主题中查找字符串,vba,outlook,Vba,Outlook,我正在尝试创建一个简单的宏,它读取活动电子邮件并检查是否存在某个字符串。现在,字符串可以有两种可能的格式,并且只包含数字 两种格式: xxx-xxxxxxxx或xxxxxxxxxx (x将始终是一个数字) 我不知道怎么做。下面我有一个宏,它读取邮件-但它只能找到一个特定的字符串: Sub AutomateReplyWithSearchString() Dim myInspector As Outlook.Inspector Dim myObject As Object
xxx-xxxxxxxx
或xxxxxxxxxx
(x将始终是一个数字)
我不知道怎么做。下面我有一个宏,它读取邮件-但它只能找到一个特定的字符串:
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
您可以使用正则表达式模式:
(\d{11}|\d{3}-\d{8})
此示例是从中复制的。我还没有测试过
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
您可以使用正则表达式模式:
(\d{11}|\d{3}-\d{8})
此示例是从中复制的。我还没有测试过
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
我认为这听起来像“RegEx”可能有用。我在这方面不是很熟练,否则我会在这里提供帮助。有更长的字符串可以包含这些字符串吗?例如,XXXXXXXXXXXX,其中x是一个数字?然后,您是只想要前11位还是后面总是有空格?例如@QHarr字符串总是11位(带或不带“-”)例如:111-10001000或11110001000。我想提取那个字符串(带或不带“-”),匹配这些字符串的基本正则表达式是(\d{11}| \d{3}-\d{8}),但是如果后面有其他数字,没有空格,匹配的部分将从这些字符串中选择。我认为这听起来像“regex”可能有用。我在这方面不是很熟练,否则我会在这里提供帮助。有更长的字符串可以包含这些字符串吗?例如,XXXXXXXXXXXX,其中x是一个数字?然后,您是只想要前11位还是后面总是有空格?例如@QHarr字符串总是11位(带或不带“-”)例如:111-10001000或11110001000。我想提取该字符串(带或不带“-”)匹配这些字符串的基本正则表达式是(\d{11}| \d{3}-\d{8}),但如果紧跟在这些字符串后面的数字没有空格,则将从这些字符串中选择匹配的部分。我可以看到,我在VBA中使用了
.find
。这可以使用正则表达式模式吗?@oliverbj否。该方法只使用字符串,这应该是有效的outlook筛选器。在您的情况下,RegExp更合适,这个答案描述了在电子邮件正文中搜索RegExep的唯一方法。。。。因此+1我可以看到我在VBA中使用了.find
。这可以使用正则表达式模式吗?@oliverbj否。该方法只使用字符串,这应该是有效的outlook筛选器。在您的情况下,RegExp更合适,这个答案描述了在电子邮件正文中搜索RegExep的唯一方法。。。。So+1