我希望回复一个网页表单,并从表单中提取电子邮件地址。
由于该网页表单位于表格中,因此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
为了澄清,这里有一张表格的图片。
Item.HTMLBody
,因为它返回一个结构化的HTML字符串,你可以用它来解析出<Table>
中适当的<TD>
元素。不要使用字符串函数来解析HTML,因为有专门设计的库更适合这个任务。否则,如果你能截屏展示一下这个邮件表格的样子,可能会有更简单的方法。 - David Zemens