Type Mismatch error返回邮件项目的属性。

huangapple go评论93阅读模式
英文:

Type Mismatch error returning mailitem property from an item

问题

我有以下的代码。

我收到

运行时错误 13 类型不匹配

objMail.ReceivedTime 处。

我尝试了 On Error Resume Next

  1. Sub ExportAttachmentsLastWeek()
  2. Dim objOL As Outlook.Application
  3. Dim objNS As Outlook.NameSpace
  4. Dim objFolder As Outlook.Folder
  5. Dim objMail As Outlook.MailItem
  6. Dim objAttachment As Outlook.Attachment
  7. Dim strFolderPath As String
  8. Dim strFileName As String
  9. Dim dtmCriteria As Date
  10. Set objOL = CreateObject("Outlook.Application")
  11. Set objNS = objOL.GetNamespace("MAPI")
  12. Set objFolder = objNS.PickFolder
  13. If objFolder Is Nothing Then
  14. Exit Sub
  15. End If
  16. strFolderPath = "C:\Users\evansj01\Documents\"
  17. ' 计算一周前的日期
  18. dtmCriteria = Now() - 7
  19. For Each objMail In objFolder.Items
  20. ' 检查电子邮件接收日期是否在上周内
  21. If objMail.ReceivedTime >= dtmCriteria Then
  22. If objMail.Attachments.Count > 0 Then
  23. For Each objAttachment In objMail.Attachments
  24. If Right(objAttachment.FileName, 3) = "xls" Then ' 根据需要更改文件格式
  25. strFileName = strFolderPath & objMail.Subject & "_" & objAttachment.FileName
  26. objAttachment.SaveAsFile strFileName
  27. End If
  28. Next
  29. End If
  30. End If
  31. Next
  32. Set objAttachment = Nothing
  33. Set objMail = Nothing
  34. Set objFolder = Nothing
  35. Set objNS = Nothing
  36. Set objOL = Nothing
  37. End Sub

我希望它能下载在该时间段内接收到的所有Excel文件到所选文件夹中。

英文:

I have this code below.

I get

>runtime error 13 type mismatch

at objMail.ReceivedTime.

I tried On Error Resume Next.

  1. Sub ExportAttachmentsLastWeek()
  2. Dim objOL As Outlook.Application
  3. Dim objNS As Outlook.NameSpace
  4. Dim objFolder As Outlook.Folder
  5. Dim objMail As Outlook.MailItem
  6. Dim objAttachment As Outlook.Attachment
  7. Dim strFolderPath As String
  8. Dim strFileName As String
  9. Dim dtmCriteria As Date
  10. Set objOL = CreateObject("Outlook.Application")
  11. Set objNS = objOL.GetNamespace("MAPI")
  12. Set objFolder = objNS.PickFolder
  13. If objFolder Is Nothing Then
  14. Exit Sub
  15. End If
  16. strFolderPath = "C:\Users\evansj01\Documents\"
  17. 'Calculate date 1 week ago
  18. dtmCriteria = Now() - 7
  19. For Each objMail In objFolder.Items
  20. 'Check if the email received date is within the last week
  21. If objMail.ReceivedTime >= dtmCriteria Then
  22. If objMail.Attachments.Count > 0 Then
  23. For Each objAttachment In objMail.Attachments
  24. If Right(objAttachment.FileName, 3) = "xls" Then 'change file format as needed
  25. strFileName = strFolderPath & objMail.Subject & "_" & objAttachment.FileName
  26. objAttachment.SaveAsFile strFileName
  27. End If
  28. Next
  29. End If
  30. End If
  31. Next
  32. Set objAttachment = Nothing
  33. Set objMail = Nothing
  34. Set objFolder = Nothing
  35. Set objNS = Nothing
  36. Set objOL = Nothing
  37. End Sub

I was hoping it would download all Excel files received in that time period to the folder selected.

答案1

得分: 2

尝试这个(从您的代码中有两处更改,都已注释):

  1. Sub ExportAttachmentsLastWeek()
  2. Dim objOL As Outlook.Application
  3. Dim objNS As Outlook.NameSpace
  4. Dim objFolder As Outlook.Folder
  5. Dim objMail As Object ' changed from Outlook.MailItem
  6. Dim objAttachment As Outlook.Attachment
  7. Dim strFolderPath As String
  8. Dim strFileName As String
  9. Dim dtmCriteria As Date
  10. Set objOL = CreateObject("Outlook.Application")
  11. Set objNS = objOL.GetNamespace("MAPI")
  12. Set objFolder = objNS.PickFolder
  13. If objFolder Is Nothing Then
  14. Exit Sub
  15. End If
  16. strFolderPath = "C:\Users\evansj01\Documents\"
  17. ' 计算一周前的日期
  18. dtmCriteria = Now() - 7
  19. For Each objMail In objFolder.Items
  20. ' 针对项目类型进行额外测试
  21. If TypeOf objMail Is MailItem Then
  22. ' 检查电子邮件接收日期是否在上周内
  23. If objMail.ReceivedTime >= dtmCriteria Then
  24. If objMail.Attachments.Count > 0 Then
  25. For Each objAttachment In objMail.Attachments
  26. If Right(objAttachment.FileName, 3) = "xls" Then ' 根据需要更改文件格式
  27. strFileName = strFolderPath & objMail.Subject & "_" & objAttachment.FileName
  28. objAttachment.SaveAsFile strFileName
  29. End If
  30. Next
  31. End If
  32. End If
  33. End If
  34. Next
  35. Set objAttachment = Nothing
  36. Set objMail = Nothing
  37. Set objFolder = Nothing
  38. Set objNS = Nothing
  39. Set objOL = Nothing
  40. End Sub

请注意,这是您提供的VBA代码的中文翻译。如果您需要进一步的帮助或解释,请告诉我。

英文:

Try this (two changes from your code, both commented):

  1. Sub ExportAttachmentsLastWeek()
  2. Dim objOL As Outlook.Application
  3. Dim objNS As Outlook.NameSpace
  4. Dim objFolder As Outlook.Folder
  5. Dim objMail As Object ' changed from Outlook.MailItem
  6. Dim objAttachment As Outlook.Attachment
  7. Dim strFolderPath As String
  8. Dim strFileName As String
  9. Dim dtmCriteria As Date
  10. Set objOL = CreateObject("Outlook.Application")
  11. Set objNS = objOL.GetNamespace("MAPI")
  12. Set objFolder = objNS.PickFolder
  13. If objFolder Is Nothing Then
  14. Exit Sub
  15. End If
  16. strFolderPath = "C:\Users\evansj01\Documents\"
  17. 'Calculate date 1 week ago
  18. dtmCriteria = Now() - 7
  19. For Each objMail In objFolder.Items
  20. ' additional test for type of item
  21. If TypeOf objMail Is MailItem Then
  22. 'Check if the email received date is within the last week
  23. If objMail.ReceivedTime >= dtmCriteria Then
  24. If objMail.Attachments.Count > 0 Then
  25. For Each objAttachment In objMail.Attachments
  26. If Right(objAttachment.FileName, 3) = "xls" Then 'change file format as needed
  27. strFileName = strFolderPath & objMail.Subject & "_" & objAttachment.FileName
  28. objAttachment.SaveAsFile strFileName
  29. End If
  30. Next
  31. End If
  32. End If
  33. End If
  34. Next
  35. Set objAttachment = Nothing
  36. Set objMail = Nothing
  37. Set objFolder = Nothing
  38. Set objNS = Nothing
  39. Set objOL = Nothing
  40. End Sub

huangapple
  • 本文由 发表于 2023年6月26日 19:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定