VBA运行时错误'-2147221233(8004010f)和#x27;

VBA运行时错误'-2147221233(8004010f)和#x27;,vba,excel,Vba,Excel,我正在尝试运行下面提到的VBA代码 VBA代码用于从Microsoft Outlook中用户选择的单个文件夹中提取电子邮件信息,并在Microsoft Excel中列出响应时间 这是我尝试运行时收到的错误消息 “运行时错误“-2147221233(8004010f)”: 属性“”未知或找不到 以下是我正在使用的代码: Option Explicit Public ns As Outlook.Namespace Private Const EXCHIVERB_REPLYTOSENDER =

我正在尝试运行下面提到的VBA代码

VBA代码用于从Microsoft Outlook中用户选择的单个文件夹中提取电子邮件信息,并在Microsoft Excel中列出响应时间

这是我尝试运行时收到的错误消息


“运行时错误“-2147221233(8004010f)”:

属性“”未知或找不到


以下是我正在使用的代码:

Option Explicit

Public ns As Outlook.Namespace

Private Const EXCHIVERB_REPLYTOSENDER = 102
Private Const EXCHIVERB_REPLYTOALL = 103
Private Const EXCHIVERB_FORWARD = 104

Private Const PR_LAST_VERB_EXECUTED = "http://schemas.microsoft.com/mapi/proptag/0x10810003"
Private Const PR_LAST_VERB_EXECUTION_TIME = "http://schemas.microsoft.com/mapi/proptag/0x10820040"
Private Const PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Private Const PR_RECEIVED_BY_ENTRYID As String = "http://schemas.microsoft.com/mapi/proptag/0x003F0102"

' Locates best matching reply in related conversation to the given mail message passed in as oMailItem
Private Function GetReply(oMailItem As MailItem) As MailItem
    Dim conItem As Outlook.Conversation
    Dim ConTable As Outlook.Table
    Dim ConArray() As Variant
    Dim MsgItem As MailItem
    Dim lp As Long
    Dim LastVerb As Long
    Dim VerbTime As Date
    Dim Clockdrift As Long
    Dim OriginatorID As String

    Set conItem = oMailItem.GetConversation ' Let Outlook and Exchange do the hard lifting to get entire converstion for email being checked.
    OriginatorID = oMailItem.PropertyAccessor.BinaryToString(oMailItem.PropertyAccessor.GetProperty(PR_RECEIVED_BY_ENTRYID))

    If Not conItem Is Nothing Then ' we have a conversation in which we should be able to match the reply
        Set ConTable = conItem.GetTable
        ConArray = ConTable.GetArray(ConTable.GetRowCount)
        LastVerb = oMailItem.PropertyAccessor.GetProperty(PR_LAST_VERB_EXECUTED)
        Select Case LastVerb
            Case EXCHIVERB_REPLYTOSENDER, EXCHIVERB_REPLYTOALL ', EXCHIVERB_FORWARD ' not interested in forwarded messages
                VerbTime = oMailItem.PropertyAccessor.GetProperty(PR_LAST_VERB_EXECUTION_TIME)
                VerbTime = oMailItem.PropertyAccessor.UTCToLocalTime(VerbTime) ' convert to local time
                ' Debug.Print "Reply to " & oMailItem.Subject & " sent on (local time): " & VerbTime
                For lp = 0 To UBound(ConArray)
                    If ConArray(lp, 4) = "IPM.Note" Then ' it is a mailitem
                        Set MsgItem = ns.GetItemFromID(ConArray(lp, 0)) 'mail item to check against
                        If Not MsgItem.Sender Is Nothing Then
                            If OriginatorID = MsgItem.Sender.ID Then
                                Clockdrift = DateDiff("s", VerbTime, MsgItem.SentOn)
                                If Clockdrift >= 0 And Clockdrift < 300 Then ' Allow for a clock drift of up to 300 seconds. This may be overgenerous
                                    Set GetReply = MsgItem
                                    Exit For ' only interested in first matching reply
                                End If
                            End If
                        End If
                    End If
                Next
            Case Else
        End Select
    End If
    ' as we exit function GetMsg is either Nothing or the reply we are interested in
End Function



Public Sub ListIt()
    Dim myOlApp As New Outlook.Application
    Dim myItem As Object ' item may not necessarily be a mailitem
    Dim myReplyItem As Outlook.MailItem
    Dim myFolder As Folder
    Dim xlRow As Long

    Set ns = myOlApp.GetNamespace("MAPI") ' Initialise Outlook access
    Set myFolder = ns.PickFolder() ' for the sake of this example we just pick a folder.

    InitSheet ActiveSheet ' initialise the spreadsheet

    xlRow = 3
    For Each myItem In myFolder.Items
        If myItem.Class = olMail Then
            Set myReplyItem = GetReply(myItem) ' this example only deals with mailitems
            If Not myReplyItem Is Nothing Then ' we found a reply
                PopulateSheet ActiveSheet, myItem, myReplyItem, xlRow
                xlRow = xlRow + 1
            End If
        End If
        DoEvents ' cheap and nasty way to allow other things to happen
    Next

    MsgBox "Done"

End Sub



Private Sub InitSheet(mySheet As Worksheet)
    With mySheet
        .Cells.Clear
        .Cells(1, 1).FormulaR1C1 = "Received"
        .Cells(2, 1).FormulaR1C1 = "From"
        .Cells(2, 2).FormulaR1C1 = "Subject"
        .Cells(2, 3).FormulaR1C1 = "Date/Time"
        .Cells(1, 4).FormulaR1C1 = "Replied"
        .Cells(2, 4).FormulaR1C1 = "From"
        .Cells(2, 5).FormulaR1C1 = "To"
        .Cells(2, 6).FormulaR1C1 = "Subject"
        .Cells(2, 7).FormulaR1C1 = "Date/Time"
        .Cells(2, 8).FormulaR1C1 = "Response Time"
    End With
End Sub



Private Sub PopulateSheet(mySheet As Worksheet, myItem As MailItem, myReplyItem As MailItem, xlRow As Long)
    Dim recips() As String
    Dim myRecipient As Outlook.Recipient
    Dim lp As Long

    With mySheet
        .Cells(xlRow, 1).FormulaR1C1 = myItem.SenderEmailAddress
        .Cells(xlRow, 2).FormulaR1C1 = myItem.Subject
        .Cells(xlRow, 3).FormulaR1C1 = myItem.ReceivedTime
        '.Cells(xlRow, 4).FormulaR1C1 = myReplyItem.SenderEmailAddress
        .Cells(xlRow, 4).FormulaR1C1 = myReplyItem.Sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) ' I prefer to see the SMTP address
        For lp = 0 To myReplyItem.Recipients.Count - 1
            ReDim Preserve recips(lp) As String
            recips(lp) = myReplyItem.Recipients(lp + 1).Address
        Next
        .Cells(xlRow, 5).FormulaR1C1 = Join(recips, vbCrLf)
        .Cells(xlRow, 6).FormulaR1C1 = myReplyItem.Subject
        .Cells(xlRow, 7).FormulaR1C1 = myReplyItem.SentOn
        .Cells(xlRow, 8).FormulaR1C1 = "=RC[-1]-RC[-5]"
        .Cells(xlRow, 8).NumberFormat = "[h]:mm:ss"
    End With
End Sub
选项显式
作为Outlook.Namespace的公共ns
Private Const EXCHIVERB_REPLYTOSENDER=102
专用Const EXCHIVERB_REPLYTOALL=103
专用Const EXCHIVERB_FORWARD=104
Private Const PR_LAST_VERB_EXECUTED=”http://schemas.microsoft.com/mapi/proptag/0x10810003"
私有常量PR_LAST_VERB_EXECUTION_TIME=”http://schemas.microsoft.com/mapi/proptag/0x10820040"
专用常量PR_SMTP_地址=”http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
私有常量PR\u由\u ENTRYID以字符串形式接收http://schemas.microsoft.com/mapi/proptag/0x003F0102"
'在作为oMailItem传入的给定邮件的相关会话中查找最匹配的回复
私有函数GetReply(oMailItem作为MailItem)作为MailItem
与前景、对话等相关的内容
与Outlook.Table一样暗显ConTable
Dim ConArray()作为变量
将MsgItem设置为邮件项
变暗lp为长
动词越长越好
将时间设置为日期
暗时钟漂移一样长
将原始ID设置为字符串
Set-conItem=oMailItem.GetConversation'让Outlook和Exchange进行艰苦的工作,以便检查电子邮件的整个对话。
OriginatorID=oMailItem.PropertyAccessor.BinaryToString(oMailItem.PropertyAccessor.GetProperty(由ENTRYID接收的请购单))
如果不是,conItem什么都不是,那么“我们有一个对话,我们应该能够匹配回答
Set ConTable=conItem.GetTable
ConArray=ConTable.GetArray(ConTable.GetRowCount)
LastVerb=oMailItem.PropertyAccessor.GetProperty(PR_LAST_VERB_EXECUTED)
选择大小写动词
大小写EXCHIVERB\u REPLYTOSENDER,EXCHIVERB\u REPLYTOALL',EXCHIVERB\u FORWARD'对转发的消息不感兴趣
VerbTime=oMailItem.PropertyAccessor.GetProperty(PR\u LAST\u VERB\u EXECUTION\u TIME)
VerbTime=oMailItem.PropertyAccessor.UTCToLocalTime(VerbTime)'转换为本地时间
'Debug.Print“回复”&oMailItem.Subject&“发送日期(本地时间):”&VerbTime
对于lp=0到uBond(ConArray)
如果ConArray(lp,4)=“IPM.Note”,则“它是一个邮件项”
设置MsgItem=ns.GetItemFromID(ConArray(lp,0))'邮件项以进行检查
如果不是MsgItem.Sender则什么都不是
如果originorid=MsgItem.Sender.ID,则
Clockdrift=DateDiff(“s”,VerbTime,MsgItem.SentOn)
如果时钟漂移>=0且时钟漂移<300,则允许时钟漂移高达300秒。这可能过于慷慨
设置GetReply=MsgItem
退出“仅对第一个匹配答复感兴趣”
如果结束
如果结束
如果结束
如果结束
下一个
其他情况
结束选择
如果结束
'当我们退出函数GetMsg时,它要么是Nothing,要么是我们感兴趣的回复
端函数
公共子列表IT()
将MyOLAP设置为新的Outlook.Application
Dim myItem As Object’项不一定是邮件项
将myReplyItem设置为Outlook.MailItem
将myFolder设置为文件夹
长得像一排
设置ns=myOlApp.GetNamespace(“MAPI”)'初始化Outlook访问
设置myFolder=ns.PickFolder()'为了这个示例,我们只选择一个文件夹。
InitSheet ActiveSheet'初始化电子表格
xlRow=3
对于myFolder.Items中的每个myItem
如果myItem.Class=olMail,则
设置myReplyItem=GetReply(myItem)'此示例仅处理邮件项
如果不是myReplyItem什么都不是,那么“我们找到了一个答复
填充工作表活动表、myItem、myReplyItem、xlRow
xlRow=xlRow+1
如果结束
如果结束
DoEvents让其他事情发生的卑鄙肮脏的方式
下一个
MsgBox“完成”
端接头
专用子初始工作表(mySheet作为工作表)
用我的纸
.细胞,清除
.单元格(1,1).公式1c1=“已接收”
.Cells(2,1)。公式1c1=“From”
.单元格(2,2).公式1c1=“受试者”
.单元格(2,3).公式1c1=“日期/时间”
.Cells(1,4)。公式1c1=“回复”
.Cells(2,4).公式1c1=“From”
.单元格(2,5).公式1c1=“To”
.单元格(2,6).公式1c1=“受试者”
.单元格(2,7).公式1c1=“日期/时间”
.单元格(2,8).公式1c1=“响应时间”
以
端接头
私有子填充表(mySheet作为工作表,myItem作为MailItem,myReplyItem作为MailItem,xlRow作为长)
Dim recips()作为字符串
将myRecipient设置为Outlook。收件人
变暗lp为长
用我的纸
.Cells(xlRow,1).FormulaR1C1=myItem.SenderEmailAddress
.Cells(xlRow,2).FormulaR1C1=myItem.Subject
.单元格(xlRow,3).公式1c1=myItem.ReceivedTime
'.Cells(xlRow,4).FormulaR1C1=myReplyItem.SenderEmailAddress
.Cells(xlRow,4).FormulaR1C1=myReplyItem.Sender.PropertyAccessor.GetProperty(PR_SMTP_地址)'我更喜欢查看SMTP地址
对于lp=0到myReplyItem.Recipients.Count-1
ReDim将recips(lp)保留为字符串
recips(lp)=myReplyItem.Recipients(lp+1).地址
下一个
.Cells(xlRow,5).FormulaR1C1=Join(recips,vbCrLf)
.Cells(xlRow,6).FormulaR1C1=myReplyItem.Subject
.单元格(xlRow,7).公式1c1=myReplyItem.SentOn
.单元格(xlRow,8).公式1c1