Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
excel VBA从宏发送电子邮件_Vba_Excel - Fatal编程技术网

excel VBA从宏发送电子邮件

excel VBA从宏发送电子邮件,vba,excel,Vba,Excel,我正在编写一个宏以从excel工作表发送电子邮件。宏准备一些报告,然后具有为报告准备电子邮件的功能。一切正常,但当它到达发送行时,它会给我一个运行时错误-2147467259。我不知道这意味着什么,但我会感谢你的帮助 以下是该函数的代码: Function Mail_Reports(ByRef wkDate2 As String, fileDate2 As String, wkNumber2 As String, thisYear2 As String) ' Works in Excel 200

我正在编写一个宏以从excel工作表发送电子邮件。宏准备一些报告,然后具有为报告准备电子邮件的功能。一切正常,但当它到达发送行时,它会给我一个运行时错误-2147467259。我不知道这意味着什么,但我会感谢你的帮助

以下是该函数的代码:

Function Mail_Reports(ByRef wkDate2 As String, fileDate2 As String, wkNumber2 As String, thisYear2 As String)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010.
' This example sends the last saved version of the Activeworkbook object .
    Dim OutApp As Object
    Dim OutMail As Object
    Dim mailList As String
    Dim i As Long, lstRow As Long, p As Long, addressNum As Long, begRow As Long
    Dim proNam2 As String
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'On Error Resume Next
   ' Change the mail address and subject in the macro before you run it.
    For i = 1 To 5 Step 1
        mailList = ""
        lstRow = Sheets("Data").Cells(Rows.Count, i).End(xlUp).Row
        addressNum = lstRow - 16
        begRow = lstRow - addressNum
        proNam2 = Sheets("Data").Cells(16, i)
        proNam2 = Replace(proNam2, " ", "")
        For p = 1 To addressNum Step 1
            mailList = Sheets("Data").Cells(begRow + p, i) & " ; " & mailList
        Next p
        With OutMail
            .To = mailList
            '.CC = "" remove comma and use this if you want to cc anyone, can be string or variable
            '.BCC = "" remove comma and use this if you want to cc anyone, can be string or variable
            .Subject = "Test"
            .HTMLBody = "<HTML><BODY><Font Face=Calibri(Body)><p>Hi All,</p><p2>Attached to this e-mail is the test file.<p2/><br><br><p3>Best,<p3/></font></BODY></HTML>"
            .attachments.Remove 1
            .attachments.Add "C:\Documents and Settings\test.xlsx"
            .Display
            .Send
    Next i


    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Function
函数邮件报告(ByRef wkDate2为字符串,fileDate2为字符串,wkNumber2为字符串,thisYear2为字符串)
'在Excel 2000、Excel 2002、Excel 2003、Excel 2007、Excel 2010、Outlook 2000、Outlook 2002、Outlook 2003、Outlook 2007、Outlook 2010中工作。
'此示例发送Activeworkbook对象的上次保存版本。
Dim OutApp作为对象
将邮件变暗为对象
将邮件列表设置为字符串
Dim i为Long,lstRow为Long,p为Long,addressNum为Long,begRow为Long
作为字符串的Dim proNam2
Set-OutApp=CreateObject(“Outlook.Application”)
Set-OutMail=OutApp.CreateItem(0)
'出现错误时,请继续下一步
'在运行宏之前,请更改宏中的邮件地址和主题。
对于i=1到5,步骤1
mailList=“”
lstRow=工作表(“数据”)。单元格(Rows.Count,i)。结束(xlUp)。行
addressNum=lstRow-16
begRow=lstRow-addressNum
proNam2=表格(“数据”)。单元格(16,i)
proNam2=替换(proNam2,“,”)
对于p=1到addressNum步骤1
邮件列表=表格(“数据”)。单元格(begRow+p,i)&“;”和邮件列表
下一个p
发邮件
.To=邮件列表
“.CC=”“如果要抄送任何人,请删除逗号并使用它,可以是字符串或变量
“.BCC=”“如果要抄送任何人,请删除逗号并使用它,可以是字符串或变量
.Subject=“测试”
.HTMLBody=“Hi All,

附加到此电子邮件的是测试文件。

最佳,” .附件.删除1 .attachments.Add“C:\Documents and Settings\test.xlsx” .展示 .发送 接下来我 错误转到0 发送邮件=无 设置应用程序=无 端函数
你能试一下吗

  • 将报告文件保存到本地驱动器中
  • 首先使用一个电子邮件地址,因此删除for循环
  • 发送时只需一个文件/范围/工作簿
  • 删除用于签名等的html标记
  • 代码:

    • 根据OP的评论进行更新:
    查看您的电子邮件concat循环,您不必每次收到新书时都这样做,除非您的邮件列表对于每个工作簿都不同…。您可以将该循环从邮件工作簿迭代中去掉

    For p = 1 To addressNum Step 1
        mailList = Sheets("Data").Cells(begRow + p, i) & " ; " & mailList
    Next p
    

    我在这里看到的一个问题是,您可以执行以下操作:

    Set OutMail = OutApp.CreateItem(0)
    
    在发送循环之外。你应该把它移到这里:

    [...]
    For p = 1 To addressNum Step 1
      mailList = Sheets("Data").Cells(begRow + p, i) & " ; " & mailList
    Next p
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
    [...]
    
    我无法对您的特定错误发表评论,因为我不知道哪些数据将进入您的OutMail对象。但是,为了帮助您进行调试,我建议您:

  • 用发出邮件关闭
    块,用
    结束
  • 设置对Microsoft Outlook 14.0对象库的引用
  • 将OutApp声明为Outlook.Application(
    Dim OutApp作为Outlook.Application
  • 将OutMail声明为Outlook.MailItem(
    Dim OutMail为Outlook.MailItem
  • 初始化OutApp如下:
    Set-OutApp=New-Outlook.Application
  • 上述内容不是必需的(除了可能使用OutMail关闭
    块),但可能有助于诊断代码问题


    另请注意,如果您使用的是较新版本的Outlook,则安全控制可能会阻止其他应用程序(Excel、Word、Access等)发送:。

    是否发送范围、工作表、工作簿?如果不使用.Send,而只使用.Display,则您的电子邮件是否正确?如果您的第一封电子邮件发送正常,但后续的邮件发送不正常,请参见下面的回答。@bonCodigo我正在发送工作簿,对不起。@mkingston如果我使用。显示它似乎正确,但它们都不发送。@Eric_S您是否尝试过我给您的步骤:)一旦您以最简单的方式发送一本书,您就可以迭代发送其余的书。让我知道这是否有帮助。你用的是哪个版本的Outlook?至于#1,我以in结尾,一定是把它删掉了。就#2而言,我只能在列表中找到对Microsoft Outlook 12.0对象库的引用,这是我的问题吗?我需要安装什么吗?Re:
    结束我想可能是这样的。Outlook 12.0库非常好;版本仅取决于您的Outlook版本。对不起,我本想提那件事,但一定是忘了。正如我所说,这不是必须的,但在IDE中它是一个很大的帮助。这很有效,所以我用HTML和FOR循环尝试了它,它用HTML代码工作,但当涉及到FOR循环时它就不起作用了……你知道如何解决这个问题吗?我应该为循环创建另一个函数吗?@Eric_我们暂时不要使用
    html
    。由于这是一次发送,让我们先保存这些书,然后尝试通过循环发送。这里可能有两件事出了问题<代码>1。
    您的ReceiPoint电子邮件字符串在迭代时可能未生成兼容的电子邮件列表<代码>2。
    html
    引起了一些问题。因此,如果您只需要连接电子邮件列表,那么这里的事情就更简单了:)因此,每个工作簿都有一个循环要迭代。那么我建议你分开看邮件。并尝试在工作簿循环中发送工作簿。事实上,请看我的更新。它确实是第一名。我有一个额外的。在那里,我把它扔掉了…现在一切似乎都好了,谢谢你的帮助。
    [...]
    For p = 1 To addressNum Step 1
      mailList = Sheets("Data").Cells(begRow + p, i) & " ; " & mailList
    Next p
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
    [...]