Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Outlook收件箱中读取邮件失败项目以将其保存在excel工作表中_Excel_Vba_Email - Fatal编程技术网

如何从Outlook收件箱中读取邮件失败项目以将其保存在excel工作表中

如何从Outlook收件箱中读取邮件失败项目以将其保存在excel工作表中,excel,vba,email,Excel,Vba,Email,我正在发送电子邮件,希望阅读失败的电子邮件并将其保存在excel工作表中。我能读懂主题、正文附件、文件名等等。我只想阅读电子邮件,并在图标附近重新放置文本,如所附快照所示。如果有人能提供帮助,请附上代码 首先添加参考:收费->参考->Microsoft VBScript正则表达式5.5 Sub GetInboxItems() Dim myFolder As MAPIFolder Dim Item As Outlook.MailItem 'MailItem Dim xlApp As Objec

我正在发送电子邮件,希望阅读失败的电子邮件并将其保存在excel工作表中。我能读懂主题、正文附件、文件名等等。我只想阅读电子邮件,并在图标附近重新放置文本,如所附快照所示。如果有人能提供帮助,请附上代码


首先添加参考:收费->参考->Microsoft VBScript正则表达式5.5

Sub GetInboxItems()

Dim myFolder As MAPIFolder
Dim Item As Outlook.MailItem 'MailItem
Dim xlApp As Object 'Excel.Application
Dim xlWB As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim Lines() As String
Dim i As Integer, x As Integer, P As Integer
Dim myItem As Variant
Dim subjectOfEmail As String
Dim bodyOfEmail As String
Dim q
Dim Results As MatchCollection
Dim match_ as Match

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

'Try access to excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Application.Visible = True

    If xlApp Is Nothing Then
        MsgBox "Excel is not accessable"
        Exit Sub
    End If
End If
On Error GoTo 0

'Set ol = New Outlook.Application
'Set ns = ol.GetNamespace("MAPI")
'Set fol = ns.GetDefaultFolder(olFolderInbox)

'Add a new workbook
Set xlWB = xlApp.Workbooks.Add
xlApp.Application.Visible = True
Set xlSheet = xlWB.ActiveSheet
Set myFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each myItem In myFolder.Items
    subjectOfEmail = myItem.Subject

    'Search for Undeliverable email
    If subjectOfEmail Like "*Delivery*" & "*failed*" Then
        bodyOfEmail = myItem.Body

        x = x + 1
        'Extract email address from email body

        strPattern = "[a-z0-9-.+_]+@[a-z-]+\.[a-z]+"

        Set Results = RegEx(bodyOfEmail, strPattern, , True, True)

        If Not Results Is Nothing Then
            For Each match_ in Results
                cells(1,1).Value = match_
            Next
        End If
    End If
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

Function RegEx(strInput As String, strPattern As String, _
Optional GlobalSearch As Boolean, Optional MultiLine As Boolean, _
Optional IgnoreCase As Boolean) As MatchCollection

Dim mcolResults As MatchCollection
Dim objRegEx As New RegExp

If strPattern <> vbNullString Then

    With objRegEx
        .Global = GlobalSearch
        .MultiLine = MultiLine
        .IgnoreCase = IgnoreCase
        .Pattern = strPattern
    End With

    If objRegEx.Test(strInput) Then
        Set mcolResults = objRegEx.Execute(strInput)
        Set RegEx = mcolResults
    End If
End If
End Function
Sub-GetInboxItems()
将myFolder设置为MAPIFolder
将项目设置为Outlook.MailItem“MailItem”
Dim xlApp作为对象的Excel.Application
Dim xlWB作为对象的Excel.工作簿
将xlSheet作为对象的Excel.Worksheet
将线()变暗为字符串
尺寸i为整数,x为整数,P为整数
Dim myItem作为变体
作为字符串的Dim subjectOfEmail
将邮件的正文设置为字符串
暗q
将结果设置为匹配集合
暗匹配作为匹配
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
Application.EnableEvents=False
'尝试访问excel
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果xlApp什么都不是,那么
设置xlApp=CreateObject(“Excel.Application”)
xlApp.Application.Visible=True
如果xlApp什么都不是,那么
MsgBox“Excel不可访问”
出口接头
如果结束
如果结束
错误转到0
'Set ol=New Outlook.Application
'Set ns=ol.GetNamespace(“MAPI”)
'Set fol=ns.GetDefaultFolder(olFolderInbox)
'添加新工作簿
设置xlWB=xlApp.Workbooks.Add
xlApp.Application.Visible=True
设置xlSheet=xlWB.ActiveSheet
设置myFolder=GetNamespace(“MAPI”).GetDefaultFolder(olFolderInbox)
对于myFolder.Items中的每个myItem
subjectOfEmail=myItem.Subject
'搜索无法送达的电子邮件
如果主题邮件类似于“*交付*”和“*失败*”,则
bodyOfEmail=myItem.Body
x=x+1
'从电子邮件正文中提取电子邮件地址
strPattern=“[a-z0-9-.+\]+@[a-z-]+\.[a-z]+.”
Set Results=RegEx(bodyOfEmail,strPattern,True,True)
如果没有结果,那就什么都不是了
对于结果中的每个匹配项
单元格(1,1)。值=匹配_
下一个
如果结束
如果结束
下一个
Application.Calculation=xlCalculationAutomatic
Application.ScreenUpdating=True
Application.EnableEvents=True
端接头
函数RegEx(strInput作为字符串,strPattern作为字符串_
可选全局搜索为布尔值,可选多行搜索为布尔值_
可选IgnoreCase(作为布尔值)作为MatchCollection
Dim mcolResults作为匹配集合
Dim objRegEx作为新的RegExp
如果strPattern vbNullString,则
用objRegEx
.Global=GlobalSearch
.MultiLine=多行
.IgnoreCase=IgnoreCase
.Pattern=strPattern
以
如果是objRegEx.Test(strInput),则
Set mcolResults=objRegEx.Execute(strInput)
设置RegEx=mcolResults
如果结束
如果结束
端函数
通过

Sub GetInboxItems()

Dim myFolder As MAPIFolder
Dim Item As Outlook.MailItem 'MailItem
Dim xlApp As Object 'Excel.Application
Dim xlWB As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim Lines() As String
Dim i As Integer, x As Integer, P As Integer
Dim myItem As Variant
Dim subjectOfEmail As String
Dim bodyOfEmail As String
Dim q
Dim Results As MatchCollection
Dim match_ as Match

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

'Try access to excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Application.Visible = True

    If xlApp Is Nothing Then
        MsgBox "Excel is not accessable"
        Exit Sub
    End If
End If
On Error GoTo 0

'Set ol = New Outlook.Application
'Set ns = ol.GetNamespace("MAPI")
'Set fol = ns.GetDefaultFolder(olFolderInbox)

'Add a new workbook
Set xlWB = xlApp.Workbooks.Add
xlApp.Application.Visible = True
Set xlSheet = xlWB.ActiveSheet
Set myFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each myItem In myFolder.Items
    subjectOfEmail = myItem.Subject

    'Search for Undeliverable email
    If subjectOfEmail Like "*Delivery*" & "*failed*" Then
        bodyOfEmail = myItem.Body

        x = x + 1
        'Extract email address from email body

        strPattern = "[a-z0-9-.+_]+@[a-z-]+\.[a-z]+"

        Set Results = RegEx(bodyOfEmail, strPattern, , True, True)

        If Not Results Is Nothing Then
            For Each match_ in Results
                cells(1,1).Value = match_
            Next
        End If
    End If
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

Function RegEx(strInput As String, strPattern As String, _
Optional GlobalSearch As Boolean, Optional MultiLine As Boolean, _
Optional IgnoreCase As Boolean) As MatchCollection

Dim mcolResults As MatchCollection
Dim objRegEx As New RegExp

If strPattern <> vbNullString Then

    With objRegEx
        .Global = GlobalSearch
        .MultiLine = MultiLine
        .IgnoreCase = IgnoreCase
        .Pattern = strPattern
    End With

    If objRegEx.Test(strInput) Then
        Set mcolResults = objRegEx.Execute(strInput)
        Set RegEx = mcolResults
    End If
End If
End Function