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
编辑:
哎呀,我误读了你的子名字。
你应该把它添加到你的模块的顶部,以在未来帮助自己。
Option Explicit
这个更新后的建议实际运行。
您的代码:专用子命令按钮1_Click()发送电子邮件结束子
应为:
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:
也许更容易向你展示我的意思。使用选项explicit时,必须显式声明变量。问题是,你将无法使用未声明的变量,例如,当你指的是应用程序时,这些变量会阻止你使用应用程序。
以下是您的脚本的更正版本:
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