0

这是我保存在记事本中的代码。我需要更改 Excel.Applications 吗?

Option Explicit

Dim xlApp
Dim xlBook

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("H:\shane.xlsm", 0, True)

xlApp.Run "Email"
xlBook.close
xlApp.Quit

Set xlBook = Nothing
Set xlApp = nothing

这是我必须发送电子邮件的代码,当我测试它工作正常并会向我发送电子邮件。

Option Explicit

Const strTo As String = "dvandervieren@enerplus.com"
Const strCC As String = ""  '<~~ change "def@abc.com" to "" if you do not want to CC
Const strBCC As String = "" '<~~ change "ghi@abc.com" to "" if you do not want to BCC

Sub Email()
Dim OutApp As Object, OutMail As Object
Dim strbody As String, strSubject As String

strSubject = "Hello World"
strbody = "This is the message for the body"

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = strTo
    .CC = strCC
    .BCC = strBCC
    .Subject = "This is the Subject line"
    .Body = strbody
    .Send
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub
4

1 回答 1

0

打开您的 excel 文档。打开 VB 编辑器。在左侧窗格中找到 excel 文档。右键单击并选择插入>>模块。将您的代码移动到新创建的模块中。然后,您应该能够仅使用方法名称 Email 来调用它。您不需要删除 excel 应用程序,因为您已经在 excel 中。– 巫术

于 2015-03-06T02:04:29.713 回答