Private Sub CommandButton1_Click()
send email
End Sub
Public Function sendmail()
On Error GoTo ende
esubject = "Systematic and Manually Created ASN"
sendto = "oo@hp.com" ccto = "rt@hp.com"
ebody = "Hello All" & vbCrLf & _
"Please find the Systematically and Manually created ASN for the last month" & _
vbCrLf & "With Regards" & vbCrLf & "Tarak"
newfilename = "C:\Stuff.XLS"
Set apps = CreateObject("Outlook.Application")
Set itm = app.createitem(0)
With itm
.Subject = esubject
.to = sendto
.cc = ccto
.body = ebody
.attachments.Add (newfilename)
.display
.Send
End With
Set app = Nothing
Set itm = Nothing
ende:
End Function
问问题
4982 次
1 回答
2
编辑:
糟糕,我误读了您的子名称。
您应该将其添加到模块的顶部,以在将来帮助自己。
Option Explicit
这个更新的建议实际上运行。
您的代码: Private Sub CommandButton1_Click() 发送电子邮件 End Sub
应该读:
Private Sub CommandButton1_Click()
sendmail
End Sub
请注意从发送电子邮件到发送邮件的变化。
此外:
Set apps = CreateObject("Outlook.Application")
Set itm = app.createitem(0)
应该读
Set apps = CreateObject("Outlook.Application")
Set itm = apps.createitem(0)
注意缺少的 s。如果没有那个 s,代码会在使用 itm 之后立即出错。
编辑2:
也许更容易告诉你我的意思。当您使用选项显式时,您必须显式声明您的变量。优点是您将无法使用尚未声明的变量,这将使您无法使用应用程序,例如,当您指的是应用程序时。
这是您的脚本的更正版本:
Option Explicit
Private Sub CommandButton1_Click()
sendmail
End Sub
Public Function sendmail()
On Error GoTo ende
Dim esubject As String, sendto As String, ccto As String, ebody As String, newfilename As String
Dim apps As Object, itm As Object
esubject = "Systematic and Manually Created ASN"
sendto = "oo@hp.com"
ccto = "rt@hp.com"
ebody = "Hello All" & vbCrLf & _
"Please find the Systematically and Manually created ASN for the last month" & _
vbCrLf & "With Regards" & vbCrLf & "Tarak"
newfilename = "C:\Stuff.XLS"
Set apps = CreateObject("Outlook.Application")
Set itm = apps.createitem(0)
With itm
.Subject = esubject
.To = sendto
.cc = ccto
.body = ebody
.attachments.Add (newfilename)
.display
.Send
End With
Set apps = Nothing
Set itm = Nothing
ende:
End Function
于 2012-07-12T20:55:47.983 回答