从 .HTMLbody 中的表格中提取电子邮件地址 [英] Extract Email address from a table in .HTMLbody

查看:28
本文介绍了从 .HTMLbody 中的表格中提取电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to reply to a webform extracting the email address from the form.

网络表单位于表格中,因此 ParseTextLinePair() 函数在标签旁边的列中返回空白作为电子邮件地址.

The webform is in a table, thus the ParseTextLinePair() function returns blanks as the email address in the column next to the label.

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

How can I extract the email address from a webform?

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:UsersReply.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

一张桌子的图片来澄清.

A picture of the table to clarify.

推荐答案

你有没有研究过 VBA 中的正则表达式,我有一段时间没有研究它,但这里有一个例子.

Have you looked in to Regular Expressions in VBA, I haven't worked on it in while but here is an example.

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 = "[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}"

    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

[A-Z0-9._%+-]+@[A-Z0-9.-]+.[AZ]{2,} 描述电子邮件地址的简单模式.

[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,} Simple pattern that describes an email address.

一系列字母、数字、点、下划线、百分号和连字符,然后是一个 at 符号,然后是另一系列的字母、数字和连字符,最后是一个点和两个或更多字母

[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

这篇关于从 .HTMLbody 中的表格中提取电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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