excel 使用电子邮件,添加列表的电子邮件地址已经打开的电子邮件项目

fcwjkofz  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(164)

我目前的代码通过按下一个按钮创建一个Outlook项目,并根据一定的标准填充电子邮件地址列表。我希望代码还能做的是检查是否有当前打开的邮件项目,然后按下按钮将地址列表添加到.cc项目。我已经尝试了几次迭代,但我完全不知所措。
如果你能帮忙的话,我将不胜感激。

  1. Private Sub CommandButton15_Click()
  2. Dim OutApp As Object
  3. Dim OutMail As Object
  4. Dim emailRng As Range, cl As Range
  5. Dim sTo As String
  6. Set emailRng = Worksheets("Emails").Range("G4:G200")
  7. For Each cl In emailRng
  8. If cl.Value <> "" Then
  9. sTo = sTo & ";" & cl.Offset(, 1).Value
  10. End If
  11. Next
  12. sTo = Mid(sTo, 2)
  13. Set OutApp = CreateObject("Outlook.Application")
  14. Set OutMail = OutApp.CreateItem(0)
  15. On Error Resume Next
  16. With OutMail
  17. .To = sTo
  18. .Display
  19. End With
  20. On Error GoTo 0
  21. End Sub

字符串

fslejnso

fslejnso1#

尝试下面的代码,我已经添加了一个Select Case,它具有各种逻辑,具体取决于您打开的Outlook邮件的数量。我已经为代码添加了注解,以使其具有一定的意义。

  • 修改代码 *
  1. Private Sub CommandButton15_Click()
  2. Dim OutApp As Object
  3. Dim OutMail As Object
  4. Dim OutInspector As Object
  5. Dim OutOpenObjCount As Long, i As Long, EmailMsgCount As Long
  6. Dim emailRng As Range, cl As Range
  7. Dim sTo As String
  8. Set emailRng = Worksheets("Emails").Range("G4:G200")
  9. For Each cl In emailRng
  10. If cl.Value <> "" Then
  11. sTo = sTo & ";" & cl.Offset(, 1).Value
  12. End If
  13. Next
  14. sTo = Mid(sTo, 2)
  15. ' --- Check if there's an Open Email Message ---
  16. ' check if Outlook already open
  17. On Error Resume Next
  18. Set OutApp = GetObject(, "Outlook.Application")
  19. On Error GoTo 0
  20. If OutApp Is Nothing Then
  21. ' create Outlook object before the loop
  22. Set OutApp = CreateObject("Outlook.Application")
  23. End If
  24. EmailMsgCount = 0 ' reset count
  25. ' check number of Outlook open objects, include msg, meetings, contacts
  26. OutOpenObjCount = OutApp.Inspectors.Count
  27. For i = 1 To OutOpenObjCount
  28. Set OutInspector = OutApp.Inspectors.Item(i)
  29. ' check if type of outlook item is a message
  30. If OutInspector.CurrentItem.Class = 43 Then ' Numeric value of olMail
  31. EmailMsgCount = EmailMsgCount + 1 ' increase count of open email messages
  32. Set OutMail = OutInspector.CurrentItem
  33. End If
  34. Next i
  35. ' - Main Logic depending of # of Open Messages in Outlook
  36. Select Case EmailMsgCount
  37. Case Is > 1 ' more than 1 open message --> can't determin automaticaal which one to refer to
  38. MsgBox "You have " & EmailMsgCount & " open email messages in Outlook"
  39. Case 0 ' none open --> create new Message
  40. ' your original code goes here
  41. Set OutMail = OutApp.CreateItem(0)
  42. With OutMail
  43. .to = sTo
  44. .Display
  45. End With
  46. Case 1 ' Use the only 1 message open
  47. With OutMail
  48. .to = .to & ";" & sTo ' CONCAT new Addresses to existing email addresses
  49. .Display
  50. End With
  51. End Select
  52. End Sub

字符串

展开查看全部

相关问题