[英]Error Excel Macro send email code of worksheet
Basically, I want to send an email after I submit a macro button on the Excel worksheet. 基本上,我想在Excel工作表上提交宏按钮后发送电子邮件。 It will then send an email of the current worksheet to one of my email addresses.
然后,它会将当前工作表的电子邮件发送到我的某个电子邮件地址。
I tried researching to see if this was a bit of old code but no luck 我试着研究一下,这是不是有点旧代码,但没有运气
Public Sub Export()
a = MsgBox("Are you sure you want to save & submit the report?", vbYesNo + vbQuestion)
If a = vbYes Then
Dim OutApp As Object
Dim OutMail As Object
Dim sTo As String: sTo = "health-safety@example.com"
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
If Dir("\\dpfbhfap003\DP-CLD-Shares\CLD-Health and Safety\", vbDirectory) = "" Then
ThisWorkbook.SaveAs "C:\Users\" & Environ("UserName") & "\Desktop\FARA - " & shtAssess.Range("sLoc") & " - " & Format(shtAssess.Range("sDate"), "yyyymmdd") & ".xlsm"
On Error Resume Next
With OutMail
.To = sTo
.CC = ""
.BCC = ""
.Subject = ThisWorkbook.Name
.Body = "User did not have access to the ""\\dpfbhfap003\DP-CLD-Shares\CLD-Health and Safety\02_FARA\"" folder when exporting the file, so was unable to save a copy there."
.Attachments.Add ThisWorkbook.FullName
.Send
' .Display
End With
On Error GoTo 0
Else
ThisWorkbook.SaveAs "\\dpfbhfap003\DP-CLD-Shares\CLD-Health and Safety\02_FARA\FARA - " & shtAssess.Range("sLoc") & " - " & Format(shtAssess.Range("sDate"), "yyyymmdd") & ".xlsm"
On Error Resume Next
With OutMail
.To = sTo
.CC = ""
.BCC = ""
.Subject = ThisWorkbook.Name
.Body = ""
.Attachments.Add ThisWorkbook.FullName
.Send
' .Display
End With
On Error GoTo 0
End If
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
a = MsgBox("Report has been successfully saved and emailed.", vbOKOnly + vbInformation, "Complete")
End If
End Sub
comes up saying 上来说
"Run-time error '425'" ActiveX component can't create object
“运行时错误'425'”ActiveX组件无法创建对象
and then it highlights this code 然后它突出显示了这段代码
Set OutApp = CreateObject("Outlook.Application")
I was able to successfully run your code with no problems. 我能够毫无问题地成功运行您的代码。 I don't see anything code related that would be an issue.
我没有看到任何与此相关的代码问题。
Make sure Excel and outlook are installed and updated and check this link if you haven't already read up on the error code. 确保安装并更新了Excel和Outlook,如果您还没有阅读过错误代码,请查看此链接。 https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/activex-component-can-t-create-object-or-return-reference-to-this-object-error-4
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/activex-component-can-t-create-object-or-return-reference-to-this-对象的错误-4-
You need something like this. 你需要这样的东西。
Sub Mail_workbook_Outlook_1()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "email1@gmail.com"
.CC = "email2@gmail.com"
.BCC = ""
.Subject = "Environmental Reporting"
.body = "Hi," & vbNewLine & vbNewLine & "Please find attached the report." & vbNewLine & vbNewLine
.Attachments.Add ActiveWorkbook.FullName
.Display
End With
Kill Template
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.