使用macros从Microsoft Outlook Messsage中提取所有IP地址

我一直在研究如何从Outlook邮件中提取所有 IP地址并复制到Excel电子表格。 我有一个工作示例,从OL消息中提取1个IP地址以复制到Excel单元。 目前它每个单元复制1个八位字节,但理想情况下我需要1个单元中的IP地址。

另外我需要macros来检查消息的全部内容并提取所有的IP地址。 消息中可能有1到100个IP地址。

样本数据

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis 10.1.1.10 aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum This IP has been flagged 192.168.1.1 This IP has been flagged 192.168.1.2 This IP has been flagged 192.168.1.3 This IP has been flagged 192.168.1.4 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non 192.168.2.1 proident, sunt in culpa qui officia deserunt mollit anim id est laborum 

 Sub CopyToExcel(olItem As Outlook.MailItem) Dim xlApp As Object Dim xlWB As Object Dim xlSheet As Object Dim vText, vText2, vText3, vText4, vText5 As Variant Dim sText As String Dim rCount As Long Dim bXStarted As Boolean Dim enviro As String Dim strPath As String Dim Reg1 As Object Dim M1 As Object Dim M As Object enviro = CStr(Environ("USERPROFILE")) 'the path of the workbook strPath = enviro & "\Documents\test.xlsx" On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Err <> 0 Then Application.StatusBar = "Please wait while Excel source is opened ... " Set xlApp = CreateObject("Excel.Application") bXStarted = True End If On Error GoTo 0 'Open the workbook to input the data Set xlWB = xlApp.Workbooks.Open(strPath) Set xlSheet = xlWB.Sheets("Sheet1") 'Find the next empty line of the worksheet rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row rCount = rCount + 1 sText = olItem.Body Set Reg1 = CreateObject("VBScript.RegExp") ' \s* = invisible spaces ' \d* = match digits ' \w* = match alphanumeric With Reg1 .Pattern = "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))" End With If Reg1.Test(sText) Then ' each "(\w*)" and the "(\d)" are assigned a vText variable Set M1 = Reg1.Execute(sText) For Each M In M1 vText = Trim(M.SubMatches(1)) vText2 = Trim(M.SubMatches(2)) vText3 = Trim(M.SubMatches(3)) vText4 = Trim(M.SubMatches(4)) ' vText5 = Trim(M.SubMatches(5)) Next End If xlSheet.Range("B" & rCount) = vText xlSheet.Range("c" & rCount) = vText2 xlSheet.Range("d" & rCount) = vText3 xlSheet.Range("e" & rCount) = vText4 xlSheet.Range("f" & rCount) = vText5 xlWB.Close 1 If bXStarted Then xlApp.Quit End If Set M = Nothing Set M1 = Nothing Set Reg1 = Nothing Set xlApp = Nothing Set xlWB = Nothing Set xlSheet = Nothing End Sub 

你的模式实际上匹配完整的IPv4地址,你可以在这个正则expression式演示中看到它。 这意味着,你只需要抓住整个比赛,而不是子比赛。

此外,为了获得多次出现(在regex101.com,请参阅g修饰符),您需要设置Reg1.Global = True

所以,使用

 With Reg1 .Pattern = "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))" .Global = True End With 

接着

 For Each M In M1 vText = Trim(M.Value) Next 

其余的代码不难调整。