求助,vba用cdo接口发邮件,excel可以,word就发布出去
vba吧
全部回复
仅看楼主
level 1
78729883 楼主
Sub CDOSENDEMAIL()
Dim CDOMail As Variant
On Error Resume Next '出错后继续执行
Application.DisplayAlerts = False '禁用系统提示
'Application.ChangeFileAccess Mode:=xlReadOnly '将工作簿设置为只读模式 报错,所以用了下面那一句
Application.DisplayDocumentInformationPanel = True
Set CDOMail = CreateObject("CDO.Message") '创建对象
CDOMail.From = "*****" '设置发信人的邮箱
CDOMail.To = "******" '设置收信人的邮箱;
CDOMail.Subject = "" '设定邮件的主题
CDOMail.HtmlBody = ""
CDOMail.AddAttachment ThisDocument.fullname '发送本工作簿为附件,这一句一直报错
stUl = "http://schemas.microsoft.com/cdo/configuration/" '微软服务器网址
With CDOMail.Configuration.Fields
.Item(stUl & "smtpserver") = "smtp.163.com" 'SMTP服务器地址
.Item(stUl & "smtpserverport") = 25 'SMTP服务器端口
.Item(stUl & "sendusing") = 2 '发送端口
.Item(stUl & "smtpauthenticate") = 1 '远程服务器需要验证
.Item(stUl & "sendusername") = "********" '发送方邮箱名称
.Item(stUl & "sendpassword") = "********" '发送方邮箱密码
.Item(stUl & "smtpconnectiontimeout") = 60 '连接超时(秒)
.Update
End With
CDOMail.Send '执行发送
Set CDOMail = Nothing '发送成功后即时释放对象
If Err.Number = 0 Then
MsgBox "成功发送邮件", , "温馨提示" '如果没有出错,则提示发送成功
Else
MsgBox Err.Description, vbInformation, "邮件发送失败" '如果出错,则提示错误类型和错误代码
End If
Application.DisplayDocumentInformationPanel = True
'Application.ChangeFileAccess Mode:=xlReadWrite '将工作簿设置为读写模式 ,报错,所以用了上面那一句
Application.DisplayAlerts = True '恢复系统提示
End Sub
感觉是CDOMail.AddAttachment ThisDocument.fullname 有问题
还有设置只读、读写 请帮忙看一下
2022年09月01日 07点09分 1
level 1
我也遇到了,.addattachment加了附件就不行,不加附件就正常,解决不了
2025年05月19日 06点05分 4
word excel代码有点不同,当时好像是修改了不适用的代码就可以使用了
2025年05月19日 07点05分
吧务
level 11
我用vb6 Excel vba 是测试成功的
2025年05月29日 07点05分 5
word也可以,但是要把不适用的代码改掉
2025年05月30日 02点05分
吧务
level 11
用jmail 或第三方控件也行
2025年05月30日 08点05分 6
1