从.HTMLbody中的表中提取电子邮件地址

问题描述:

我想回复从表单中提取电子邮件地址的网络表单。从.HTMLbody中的表中提取电子邮件地址

Webform在表中,因此ParseTextLinePair()函数返回空白作为标签旁边列中的电子邮件地址。

如何从网络表格中提取电子邮件地址?

Sub ReplywithTemplatev2() 
Dim Item As Outlook.MailItem 
Dim oRespond As Outlook.MailItem 

'Get Email 
    Dim intLocAddress As Integer 
    Dim intLocCRLF As Integer 
    Dim strAddress As String 

Set Item = GetCurrentItem() 

If Item.Class = olMail Then 

     ' find the requestor address 
     strAddress = ParseTextLinePair(Item.Body, "Email-Adresse des Ansprechpartners *") 


' This sends a response back using a template 
Set oRespond = Application.CreateItemFromTemplate("C:\Users\Reply.oft") 

With oRespond 
    .Recipients.Add Item.SenderEmailAddress 
    .Subject = "Your Subject Goes Here" 
    .HTMLBody = oRespond.HTMLBody & vbCrLf & _ 
       "---- original message below ---" & vbCrLf & _ 
       Item.HTMLBody & vbCrLf 

' includes the original message as an attachment 
    ' .Attachments.Add Item 

    oRespond.To = strAddress 

' use this for testing, change to .send once you have it working as desired 
    .Display 


End With 

End If 
Set oRespond = Nothing 

End Sub 

Function GetCurrentItem() As Object 
    Dim objApp As Outlook.Application 

    Set objApp = Application 
    On Error Resume Next 
    Select Case TypeName(objApp.ActiveWindow) 
     Case "Explorer" 
      Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1) 
     Case "Inspector" 
      Set GetCurrentItem = objApp.ActiveInspector.CurrentItem 
    End Select 

    Set objApp = Nothing 
End Function 

Function ParseTextLinePair(strSource As String, strLabel As String) 
    Dim intLocLabel As Integer 
    Dim intLocCRLF As Integer 
    Dim intLenLabel As Integer 
    Dim strText As String 

    ' locate the label in the source text 
    intLocLabel = InStr(strSource, strLabel) 
    intLenLabel = Len(strLabel) 
     If intLocLabel > 0 Then 
     intLocCRLF = InStr(intLocLabel, strSource, vbCrLf) 
     If intLocCRLF > 0 Then 
      intLocLabel = intLocLabel + intLenLabel 
      strText = Mid(strSource, _ 
          intLocLabel, _ 
          intLocCRLF - intLocLabel) 
     Else 
      intLocLabel = Mid(strSource, intLocLabel + intLenLabel) 
     End If 
    End If 
    ParseTextLinePair = Trim(strText) 
End Function 

表中的的图象,以澄清。

enter image description here

+0

你可能有'Item.HTMLBody'更好的运气,它返回一个HTML结构串ÿ你可以用它从“

”中解析出合适的'
'元素。不要使用字符串函数来解析HTML,不过,有专为此设计的库更适合。否则,如果您可以截取此电子邮件表的外观,可能会有更简单的方法? –
+1

新增了屏幕截图。带2列和9行的表格。 – user3772665

回答

你有没有在看正则表达式在VBA,我没有工作就可以了,而在这里却是一个例子。


Option Explicit 
Sub Example() 
    Dim Item As MailItem 
    Dim RegExp As Object 
    Dim Search_Email As String 
    Dim Pattern As String  
    Dim Matches As Variant 

    Set RegExp = CreateObject("VbScript.RegExp") 

    Pattern = "\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b" 

    For Each Item In ActiveExplorer.Selection 

     Search_Email = Item.body 

     With RegExp 
      .Global = False 
      .Pattern = Pattern 
      .IgnoreCase = True 
      Set Matches = .Execute(Search_Email) 
     End With 

     If Matches.Count > 0 Then 
      Debug.Print Matches(0) 
     Else 
      Debug.Print "Not Found " 
     End If 

    Next 

    Set RegExp = Nothing 

End Sub 

或者Pattern = "(\S*@\w+\.\w+)"或者"(\w+(?:\W+\w+)*@\w+\.\w+)"


Regular-expressions.info/tutorial

\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,}\b描述电子邮件地址的简单模式。

一串字母,数字,点,下划线,百分号和连字符,接着是在符号,后跟另一系列的字母,数字和连字符,最后是一个点和两个或多个字母

[A-Z0-9._%+-]+匹配在A和Z之间的范围存在于该列表中的单个字符的下方

A-Z单个字符(区分大小写)

0-9范围的一个字符0 9

._%+-在列表中的单个字符之间和

@匹配的字符@字面上


量词

Udemy.com/vba-regex/

+---------+---------------------------------------------+------------------------------------------------------------+ 
| Pattern |     Meaning     |       Example       | 
+---------+---------------------------------------------+------------------------------------------------------------+ 
|   |            |               | 
| –  | Stands for a range       | a-z means all the letters a to z       | 
| []  | Stands for any one of the characters quoted | [abc] means either a, b or c.[A-Z] means either A, B, …, Z | 
|()  | Used for grouping purposes     |               | 
| |  | Meaning is ‘or’        | X|Y, means X or Y           | 
| +  | Matches the character one or more times  | zo+ matches ‘zoo’, but not ‘z’        | 
| *  | Matches the character zero or more times | “lo*” matches either “l” or “loo”       | 
| ?  | Matches the character zero or once   | “b?ve?” matches the “ve” in “never”.      | 
+---------+---------------------------------------------+------------------------------------------------------------+ 

Wikibooks.org/wiki/Visual_Basic/Regular_Expressions

https://regex101.com/r/oP2yR0/1

+0

非常感谢,适用于电子邮件地址。 原因我想要使用基于表格的解决方案或上述解决方案的原因是我还想从第二列的第一行检索条目。 – user3772665

+1

出现关于提取电子邮件地址的问题已得到令人满意的回答。考虑接受它并创建一个新问题。 http://*.com/help/accepted-answer – niton

+0

如何有超过1个电子邮件地址? – pablo808

相关文章

你有没有在看正则表达式在VBA,我没有工作就可以了,而在这里却是一个例子。


Option Explicit 
Sub Example() 
    Dim Item As MailItem 
    Dim RegExp As Object 
    Dim Search_Email As String 
    Dim Pattern As String  
    Dim Matches As Variant 

    Set RegExp = CreateObject("VbScript.RegExp") 

    Pattern = "\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b" 

    For Each Item In ActiveExplorer.Selection 

     Search_Email = Item.body 

     With RegExp 
      .Global = False 
      .Pattern = Pattern 
      .IgnoreCase = True 
      Set Matches = .Execute(Search_Email) 
     End With 

     If Matches.Count > 0 Then 
      Debug.Print Matches(0) 
     Else 
      Debug.Print "Not Found " 
     End If 

    Next 

    Set RegExp = Nothing 

End Sub 

或者Pattern = "(\S*@\w+\.\w+)"或者"(\w+(?:\W+\w+)*@\w+\.\w+)"


Regular-expressions.info/tutorial

\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,}\b描述电子邮件地址的简单模式。

一串字母,数字,点,下划线,百分号和连字符,接着是在符号,后跟另一系列的字母,数字和连字符,最后是一个点和两个或多个字母

[A-Z0-9._%+-]+匹配在A和Z之间的范围存在于该列表中的单个字符的下方

A-Z单个字符(区分大小写)

0-9范围的一个字符0 9

._%+-在列表中的单个字符之间和

@匹配的字符@字面上


量词

Udemy.com/vba-regex/

+---------+---------------------------------------------+------------------------------------------------------------+ 
| Pattern |     Meaning     |       Example       | 
+---------+---------------------------------------------+------------------------------------------------------------+ 
|   |            |               | 
| –  | Stands for a range       | a-z means all the letters a to z       | 
| []  | Stands for any one of the characters quoted | [abc] means either a, b or c.[A-Z] means either A, B, …, Z | 
|()  | Used for grouping purposes     |               | 
| |  | Meaning is ‘or’        | X|Y, means X or Y           | 
| +  | Matches the character one or more times  | zo+ matches ‘zoo’, but not ‘z’        | 
| *  | Matches the character zero or more times | “lo*” matches either “l” or “loo”       | 
| ?  | Matches the character zero or once   | “b?ve?” matches the “ve” in “never”.      | 
+---------+---------------------------------------------+------------------------------------------------------------+ 

Wikibooks.org/wiki/Visual_Basic/Regular_Expressions

https://regex101.com/r/oP2yR0/1

+0

非常感谢,适用于电子邮件地址。 原因我想要使用基于表格的解决方案或上述解决方案的原因是我还想从第二列的第一行检索条目。 – user3772665

+1

出现关于提取电子邮件地址的问题已得到令人满意的回答。考虑接受它并创建一个新问题。 http://*.com/help/accepted-answer – niton

+0

如何有超过1个电子邮件地址? – pablo808

你有没有在看正则表达式在VBA,我没有工作就可以了,而在这里却是一个例子。


Option Explicit 
Sub Example() 
    Dim Item As MailItem 
    Dim RegExp As Object 
    Dim Search_Email As String 
    Dim Pattern As String  
    Dim Matches As Variant 

    Set RegExp = CreateObject("VbScript.RegExp") 

    Pattern = "\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b" 

    For Each Item In ActiveExplorer.Selection 

     Search_Email = Item.body 

     With RegExp 
      .Global = False 
      .Pattern = Pattern 
      .IgnoreCase = True 
      Set Matches = .Execute(Search_Email) 
     End With 

     If Matches.Count > 0 Then 
      Debug.Print Matches(0) 
     Else 
      Debug.Print "Not Found " 
     End If 

    Next 

    Set RegExp = Nothing 

End Sub 

或者Pattern = "(\S*@\w+\.\w+)"或者"(\w+(?:\W+\w+)*@\w+\.\w+)"


Regular-expressions.info/tutorial

\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,}\b描述电子邮件地址的简单模式。

一串字母,数字,点,下划线,百分号和连字符,接着是在符号,后跟另一系列的字母,数字和连字符,最后是一个点和两个或多个字母

[A-Z0-9._%+-]+匹配在A和Z之间的范围存在于该列表中的单个字符的下方

A-Z单个字符(区分大小写)

0-9范围的一个字符0 9

._%+-在列表中的单个字符之间和

@匹配的字符@字面上


量词

Udemy.com/vba-regex/

+---------+---------------------------------------------+------------------------------------------------------------+ 
| Pattern |     Meaning     |       Example       | 
+---------+---------------------------------------------+------------------------------------------------------------+ 
|   |            |               | 
| –  | Stands for a range       | a-z means all the letters a to z       | 
| []  | Stands for any one of the characters quoted | [abc] means either a, b or c.[A-Z] means either A, B, …, Z | 
|()  | Used for grouping purposes     |               | 
| |  | Meaning is ‘or’        | X|Y, means X or Y           | 
| +  | Matches the character one or more times  | zo+ matches ‘zoo’, but not ‘z’        | 
| *  | Matches the character zero or more times | “lo*” matches either “l” or “loo”       | 
| ?  | Matches the character zero or once   | “b?ve?” matches the “ve” in “never”.      | 
+---------+---------------------------------------------+------------------------------------------------------------+ 

Wikibooks.org/wiki/Visual_Basic/Regular_Expressions

https://regex101.com/r/oP2yR0/1

+0

非常感谢,适用于电子邮件地址。 原因我想要使用基于表格的解决方案或上述解决方案的原因是我还想从第二列的第一行检索条目。 – user3772665

+1

出现关于提取电子邮件地址的问题已得到令人满意的回答。考虑接受它并创建一个新问题。 http://*.com/help/accepted-answer – niton

+0

如何有超过1个电子邮件地址? – pablo808