使用Excel 365发送Outlook电子邮件,使用旧版本的Excel

laawzig2  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(165)

我正在使用Excel电子邮件代码从Excel表中的电子邮件地址和文件名列表中发送带有多个附件的多封电子邮件。
Excel数据库Source Link:https://github.com/sotirop/mergelook
我们的IT团队将MS Excel从MS 2016更新为MS 365,并将操作系统更新为Windows 10。
现在我得到了-
'运行时错误' 287 ':应用程序定义或对象定义的错误'
以线

  1. .To = .To & "; " & ActiveSheet.Cells(row, col).Value

字符串
x1c 0d1x的数据



适用于旧版本Excel的代码。

  1. Sub sendEmailWithAttachments()
  2. Dim OutLookApp As Object
  3. Dim OutLookMailItem As Object
  4. Dim myAttachments As Object
  5. Dim row As Integer
  6. Dim col As Integer
  7. Set OutLookApp = CreateObject("Outlook.application")
  8. row = 2
  9. col = 1
  10. ActiveSheet.Cells(row, col).Select
  11. Do Until IsEmpty(ActiveCell)
  12. workFile = Application.ActiveWorkbook.Path & "\" & "message.oft"
  13. If FileExists(workFile) Then
  14. Set OutLookMailItem = OutLookApp.CreateItemFromTemplate(workFile)
  15. Else
  16. MsgBox ("message.oft file does not exist in the folder!" & vbNewLine & _
  17. "Also verify that the name is exactly 'message.oft'." & vbNewLine & _
  18. "Exiting...")
  19. Exit Sub
  20. End If
  21. Set myAttachments = OutLookMailItem.Attachments
  22. 'Do Until IsEmpty(ActiveCell)
  23. Do Until IsEmpty(ActiveSheet.Cells(1, col))
  24. With OutLookMailItem
  25. If ActiveSheet.Cells(row, col).Value = "xxxFINISHxxx" Then
  26. 'MsgBox ("Exiting...")
  27. Exit Sub
  28. End If
  29. If ActiveSheet.Cells(1, col).Value = "To" And Not IsEmpty(ActiveCell) Then
  30. .To = .To & "; " & ActiveSheet.Cells(row, col).Value
  31. ElseIf ActiveSheet.Cells(1, col).Value = "Cc" And Not IsEmpty(ActiveCell) Then
  32. .CC = .CC & "; " & ActiveSheet.Cells(row, col).Value
  33. ElseIf ActiveSheet.Cells(1, col).Value = "Bcc" And Not IsEmpty(ActiveCell) Then
  34. .BCC = .BCC & "; " & ActiveSheet.Cells(row, col).Value
  35. ElseIf ActiveSheet.Cells(1, col).Value = "Reply-To" And Not IsEmpty(ActiveCell) Then
  36. .ReplyRecipients.Add ActiveSheet.Cells(row, col).Value
  37. ElseIf ActiveSheet.Cells(1, col).Value = "attachment" And Not IsEmpty(ActiveCell) Then
  38. attachmentName = ActiveSheet.Cells(row, col).Value
  39. attachmentFile = Cells(ActiveCell.row, 17).Value & "\" & attachmentName
  40. If FileExists(attachmentFile) Then
  41. myAttachments.Add Cells(ActiveCell.row, 17).Value & "\" & ActiveSheet.Cells(row, col).Value
  42. Else
  43. MsgBox (Attachment & "'" & attachmentName & "'" & " file does not exist in the folder!" & vbNewLine & _
  44. "Correct the situation and delete all messages from Outlook's Outbox folder before pressing 'Send Emails' again!" & vbNewLine & _
  45. "Exiting...")
  46. Exit Sub
  47. End If
  48. ElseIf ActiveSheet.Cells(1, col).Value = "xxxignorexxx" Then
  49. ' Do Nothing
  50. Else
  51. .Subject = Replace(.Subject, ActiveSheet.Cells(1, col).Value, ActiveSheet.Cells(row, col).Value)
  52. 'Write #1, .HTMLBody
  53. .HTMLBody = Replace(.HTMLBody, ActiveSheet.Cells(1, col).Value, ActiveSheet.Cells(row, col).Value)
  54. 'ActiveSheet.Cells(10, 10) = .HTMLBody
  55. End If
  56. 'MsgBox (.To)
  57. End With
  58. 'Application.Wait (Now + #12:00:01 AM#)
  59. col = col + 1
  60. ActiveSheet.Cells(row, col).Select
  61. Loop
  62. OutLookMailItem.HTMLBody = Replace(OutLookMailItem.HTMLBody, "xxxNLxxx", "<br>")
  63. OutLookMailItem.send
  64. col = 1
  65. row = row + 1
  66. ActiveSheet.Cells(row, col).Select
  67. Loop
  68. End Sub

vc6uscn9

vc6uscn91#

我建议使用MailItem类的Recipients属性来设置收件人,然后调用ResolveAll方法,该方法尝试根据地址簿解析Recipients集合中的所有Recipient对象。例如:

  1. Set myRecipient = MyItem.Recipients.Add("Eugene Astafiev")
  2. myRecipient.Resolve
  3. If myRecipient .Resolved Then
  4. myItem.Subject = "Test task"
  5. myItem.Display
  6. End If

字符串
请参阅How To: Fill TO,CC and BCC fields in Outlook programmatically了解更多信息。

gzszwxb4

gzszwxb42#

扩展我上面的评论:使用To/CC/BCC属性作为中间变量是一个非常糟糕的主意。引入专用变量并构建它们。一旦你退出循环,设置To/CC/BCC属性而不必阅读它们。

  1. vTo = "";
  2. Do Until IsEmpty(ActiveSheet.Cells(1, col))
  3. ...
  4. If ActiveSheet.Cells(1, col).Value = "To" And Not IsEmpty(ActiveCell)
  5. Then
  6. vTo = vTo & "; " & ActiveSheet.Cells(row, col).Value
  7. ...
  8. Loop
  9. OutLookMailItem.To = vTo

字符串

相关问题