【发布时间】:2018-09-05 03:58:26
【问题描述】:
我正在尝试从对电子邮件爆炸的不良响应中收集电子邮件地址。
代码分为两部分,搜索部分,它在电子邮件中搜索一个字符并返回其前后的字符串,以及过程部分,它对 Outlook 文件夹中的每封电子邮件运行搜索。
我已经在复制到 Excel 中的电子邮件上测试了搜索,它可以正常工作。
我遇到的问题是我无法将作为对象的电子邮件正文传递给字符串变量。
Sub Extract()
On Error Resume Next
'specify the folder to pull emails from
Set myOlApp = Outlook.Application
Set mynamespace = myOlApp.GetNamespace("mapi")
Set myfolder = myOlApp.ActiveExplorer.CurrentFolder
Dim myitem As Outlook.MailItem
'start excel and open spreadsheet
Set xlobj = CreateObject("excel.application")
xlobj.Visible = True
xlobj.Workbooks.Add
'Set Heading on spreadsheet
xlobj.Range("a" & 1).Value = "Email"
'for loop passing email body to search code
For i = 1 To myfolder.Items.Count
Set myitem = myfolder.Items(i)
extractStr = myitem.Body
'search for specific text
CheckStr = "[A-Za-z0-9._-]"
OutStr = ""
Index = 1
Index1 = VBA.InStr(Index, extractStr, "@")
getStr = ""
If Index1 > 0 Then
For p = Index1 - 1 To 1 Step -1
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = Mid(extractStr, p, 1) & getStr
Else
Exit For
End If
Next
getStr = getStr & "@"
For p = Index1 + 1 To Len(extractStr)
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = getStr & Mid(extractStr, p, 1)
Else
Exit For
End If
Next
Index = Index1 + 1
If OutStr = "" Then
OutStr = getStr
Else
OutStr = OutStr & Chr(10) & getStr
End If
Else
GoTo 20
End If
'write to excel
20 xlobj.Range("a" & i + 1).Value = OutStr
Next
End Sub
更新:我想我已经弄明白了。为了测试这个脚本,我放置了一两封电子邮件,以便将电子邮件地址从测试文件夹中提取出来。我选择的电子邮件是 html 格式的!我放了以下代码行来将当前的电子邮件正文(myitem)转换为纯文本。
myitem.BodyFormat = olFormatPlain
我已将 myitem 变量声明为对象和邮件项。当我以 myitem 作为对象运行脚本时,在以下行中出现“对象不支持此属性或方法”错误:
myitem.BodyFormat = olFormatPlain
但是,当我将它作为邮件项目运行时,我在此行收到类型不匹配错误:
For Each myitem In myfolder
以下是我在两种不同场景中声明 myitem 变量的方式:
Dim myitem as MailItem
Dim myitem as Object
这是我更新的代码。
Option Explicit
Sub Extract()
'On Error Resume Next
'Variable declaration
Dim myOlApp As Outlook.Application
Dim mynamespace As Outlook.NameSpace
Dim myfolder As Selection
Dim myitem As MailItem
Dim i As Integer
Dim extractStr As String
Dim CheckStr As String
Dim OutStr As String
Dim Index As Integer
Dim Index1 As Integer
Dim getStr As String
Dim p As Integer
'start excel and open spreadsheet
Dim xlobj As Object
Set xlobj = CreateObject("excel.application")
xlobj.Visible = True
xlobj.Workbooks.Add
'Set Heading on spreadsheet
xlobj.Range("a" & 1).Value = "Email"
'Set reference to the Selection.
Set myOlApp = Outlook.Application
Set mynamespace = myOlApp.GetNamespace("mapi")
Set myfolder = ActiveExplorer.Selection
'for loop passing email body to search code
For Each myitem In myfolder
myitem.BodyFormat = olFormatPlain
extractStr = myitem.Body
MsgBox (extractStr)
'search for specific text
CheckStr = "[A-Za-z0-9._-]"
OutStr = ""
Index = 1
Index1 = VBA.InStr(Index, extractStr, "@")
getStr = ""
If Index1 > 0 Then
For p = Index1 - 1 To 1 Step -1
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = Mid(extractStr, p, 1) & getStr
Else
Exit For
End If
Next
getStr = getStr & "@"
For p = Index1 + 1 To Len(extractStr)
If Mid(extractStr, p, 1) Like CheckStr Then
getStr = getStr & Mid(extractStr, p, 1)
Else
Exit For
End If
Next
Index = Index1 + 1
If OutStr = "" Then
OutStr = getStr
Else
OutStr = OutStr & Chr(10) & getStr
End If
Else
GoTo 20
End If
'write to excel
20 xlobj.Range("a" & i + 1).Value = OutStr
Next
End Sub
【问题讨论】:
-
myitem.Body不返回字符串吗? -
从您的代码中删除
On Error Resume Next,您的代码中可能存在错误-您还缺少Option Explicit -
谢谢@drec4s。没错,myitem.body 应该返回一个字符串。
-
感谢@0m3r 我已经更正了变量装饰中的错误。现在我在“set myitem = myfolder.items(i)”这一行得到类型不匹配(运行时错误 13),myitem 被声明为 outlook.mailitem,而 myfolder.items(i) 实际上是 outlook.application。 activeexplorer.currentfolder.items(i) 有没有想过为什么不匹配?