简体   繁体   English

使用 Excel VBA 发送电子邮件

[英]Sending emails with Excel VBA

I have a macro (executed with a VBScript via task scehduler) that does some calculations then sends an email with a workbook attached.我有一个宏(通过任务调度程序使用VBScript执行)进行一些计算,然后发送一个带有工作簿的 email。 The problem that I'm facing is that the email is not sent when the macro is executed with the VBSCript , I get an ActiveX component can't create object: 'Outlook.Application' error on the following line: Set OutApp = CreateObject("Outlook.Application") , but the email is sent when the macro is run manually using the play button.我面临的问题是,当使用 VBSCript 执行宏时未发送VBSCript ,我得到一个ActiveX component can't create object: 'Outlook.Application' Set OutApp = CreateObject("Outlook.Application") ,但是当使用播放按钮手动运行宏时会发送 email。

The macro works fine with Office 2013 on my laptop but I'm running it on a different desktop with Office 2016 and have enabled the following reference in excel: Microsoft Outlook 16.0 Object Library but that hasn't fixed it. The macro works fine with Office 2013 on my laptop but I'm running it on a different desktop with Office 2016 and have enabled the following reference in excel: Microsoft Outlook 16.0 Object Library but that hasn't fixed it.

What might be the reason causing this behavior?导致这种行为的原因可能是什么? One thing that I've noticed is that the following error message pops up when starting outlook 2016 : The server you are connected to is using a security certificate that cannot be verified .我注意到的一件事是在启动outlook 2016时弹出以下错误消息: The server you are connected to is using a security certificate that cannot be verified Also I get a VBScript runtime error but I'm not sure that is the cause.我还收到VBScript runtime error ,但我不确定这是不是原因。

VBSCript to run macro: VBSCript 运行宏:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Reports\Daily Traffic Report per Site\Report.xlsm", , True)   'true here means readonly=yes.


objExcel.Application.Run "Report.xlsm!Email_Workbook"
objExcel.ActiveWorkbook.Close

WScript.Quit

Macro to send email:宏发送email:

Sub Email_Workbook()
'Mail a copy of the ActiveWorkbook with another file name
    Dim wb1 As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim OutApp As Object
    Dim OutMail As Object

    Set wb1 = Workbooks("Traffic Report.xlsx")

    'Make a copy of the file/Open it/Mail it/Delete it
    'If you want to change the file name then change only TempFileName
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Daily Traffic Report" & " " & Format(Now, "dd-mmm-yyyy")
    FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))

    wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
         .to = "xxx"
        .BCC = ""
        .Subject = "DIALY TRAFFIC REPORT"
        .Body = "Please find attached the Daily Traffic Report."
        .Attachments.Add TempFilePath & TempFileName & FileExtStr
        .Send
    End With
    On Error GoTo 0

    'Delete the file
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

VBScript Error: VBScript 错误:

VBScript 错误

You can skip VBscript and Task scehduler and use VBAs Application.OnTime.您可以跳过 VBscript 和任务调度器并使用 VBA 应用程序.OnTime。
This function will run a macro at a earliest time (read about the function what that means).这个 function 将在最早的时间运行一个宏(阅读 function 这意味着什么)。
If the workbook is closed, it will open the workbook to run the macro.如果工作簿已关闭,它将打开工作簿以运行宏。

Public fireTime As Date

Private Sub Workbook_Open()
    if fireTime = "00:00:00" then ' if the code has not been run before
        fireTime = TimeValue("09:00:00")
        Application.OnTime EarliestTime:=fireTime, Procedure:="Email_Workbook", Schedule:=True
        Application.displayAlerts = False
        ThisWorkbook.close
    end if
End Sub

The above will start the autorun each day at 9 each morning, then close the workbook.以上将在每天早上 9 点开始自动运行,然后关闭工作簿。 When the time is 9 the workbook will open and run Email_Workbook.当时间为 9 时,工作簿将打开并运行 Email_Workbook。

At the end of Email_Workbook I think you need to add:在 Email_Workbook 的末尾,我认为您需要添加:

fireTime = TimeValue("09:00:00")
Application.OnTime EarliestTime:=fireTime, Procedure:="Email_Workbook", Schedule:=True

To make sure it runs again next day at 9.确保它在第二天 9 点再次运行。
Now you can add in windows startup a link to this file, so that each time you start the computer, this file opens, sets next run at 9, then closes itself.现在您可以在 windows 启动中添加指向此文件的链接,这样每次启动计算机时,此文件都会打开,将下次运行设置为 9,然后自行关闭。
At 9 it runs Email_Workbook and sets the next run at 9 next day.它在 9 点运行 Email_Workbook,并将下一次运行设置为第二天 9 点。

To stop it from running you need to either restart the computer or use the command:要阻止它运行,您需要重新启动计算机或使用以下命令:

Application.OnTime EarliestTime:=fireTime, Procedure:="Email_Workbook", Schedule:=False

Not the False at the end.不是最后的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM