【问题标题】:Trouble in sending the mail to multiple recipients at the same time from outlook vba从 Outlook vba 将邮件同时发送给多个收件人的问题
【发布时间】:2020-06-23 15:36:53
【问题描述】:

我正在尝试从 Outlook vba 向多个收件人发送邮件。

收件人邮件地址取自excel表格的A列。当我运行以下代码时,出现错误“运行时错误 1004;方法‘对象单元格’_Global’失败”

如何将同一封邮件同时发送给多个收件人。

收件人:Abc@gmail.Com; bhy@gmail.com; rft@gmail.com 抄送:hjuy@gmail.com; ijk@gmail.com 主题:测试邮件

Code:

Sub Sendmail()
     Dim olItem As Outlook.MailItem
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSht As Excel.Worksheet
    Dim sPath As String
    Dim iRow As Long

        iRow = 1

    sPath = "XX"
'   // Excel
    Set xlApp = CreateObject("Excel.Application")
'   // Workbook
    Set xlBook = xlApp.Workbooks.Open(sPath)
'   // Sheet
    Set xlSht = xlBook.Sheets("Sheet1")


Do Until IsEmpty(Cells(iRow, 1))

      Recip = Cells(iRow, 1).Value
     ' subject = Cells(iRow, 2).Value
     ' Atmt = Cells(iRow, 3).Value '

'   // Create e-mail Item
    Set olItem = Application.CreateItem(olMailItem)

    With olItem
    Set olRecip = .Recipients.Add(Recip)




        .CC = xlSht.Range("B1")

        .subject = "test"
        .Display
       .Send
    End With


'   // Close
    xlBook.Close SaveChanges:=True
'   // Quit
    xlApp.Quit

    '// CleanUp


      iRow = iRow + 1

   Loop
    Set xlApp = Nothing
    Set xlBook = Nothing
    Set xlSht = Nothing
Set olItem = Nothing



End Sub

【问题讨论】:

  • 你在哪个办公室/窗口运行?
  • Windows 8.1 桌面

标签: excel vba outlook


【解决方案1】:

这应该适合你。

在 Sheets("Sheet1") 中使用:

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

宏将遍历“Sheet1”中的每一行,如果 B 列中有电子邮件地址 和 C:Z 列中的文件名,它将使用此信息创建一封邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

https://www.rondebruin.nl/win/s1/outlook/amail6.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2017-09-26
    • 2014-02-06
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2018-11-19
    • 2013-04-03
    相关资源
    最近更新 更多