Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 12936|回复: 11

vba调用outlook的问题?

[复制链接]
发表于 2008-8-27 09:19 | 显示全部楼层 |阅读模式

 Set   oApp   =   Outlook.Application   
 Set   oMessage   =   oApp.CreateItem(olMailItem)   
 oMessage.To   =   "test@test.com"   
 oMessage.Subject   =   "Test   Sending   Email   with   Attachment"   
 oMessage.Attachments.Add   ("C:\Test.doc")   
 oMessage.Send   
 Set   oMessage   =   Nothing   
 Set   oApp   =   Nothing

我网络上找到的代码,可在运行时报:编译错误 缺少函数或变量 

发表于 2008-8-27 09:24 | 显示全部楼层

使用时,把这段代码加入到过程中,才能调用

回复

使用道具 举报

 楼主| 发表于 2008-8-27 09:29 | 显示全部楼层

我就是在过程里的:

sub mail

Set   oApp   =   Outlook.Application   
 Set   oMessage   =   oApp.CreateItem(olMailItem)   
 oMessage.To   =   "test@test.com"   
 oMessage.Subject   =   "Test   Sending   Email   with   Attachment"   
 oMessage.Attachments.Add   ("C:\Test.doc")   
 oMessage.Send   
 Set   oMessage   =   Nothing   
 Set   oApp   =   Nothing

end sub

回复

使用道具 举报

 楼主| 发表于 2008-8-27 19:54 | 显示全部楼层

Sub outlook()
Dim stUl As String      '微软服务器网址
Dim vCDO As Variant     'CDO.Message对象
Dim stUs As String      '发送方邮箱名称
Dim stRx As String      '发送方邮箱服务器
Dim stPw As String      '发送方邮箱密码
Dim stE1 As String      '主要接收方邮箱完整帐号
Dim stE2 As String      '备用接收方邮箱完整帐号
Dim stZt As String      '邮件主题
Dim stNr As String      '邮件内容
Dim stFj As String      '邮件附件

stUs = Trim("zhang") '发件人用户名
stRx = Trim("zhangyl@oa.163.com") '发送邮箱
stPw = Trim("zhangyile") '发件人密码
stE1 = Trim("chenlg@oa.163.com") '收件人用户名
stZt = Trim("你好")
'stNr = Trim(Nz(Me.内容))
stFj = Trim("c:\程序.xls")

'stUl = "http://schemas.microsoft.com/cdo/configuration/" '微软服务器网址

Set vCDO = CreateObject("CDO.Message")                   '建立对象

vCDO.From = stRx                                         '发送方邮箱完整帐号
vCDO.To = stE1                                           '主要接收方邮箱完整帐号
If Len(stE2) > 0 Then vCDO.CC = stE2                     '备用接收方邮箱完整帐号
vCDO.Subject = stZt                                      '邮件主题
vCDO.Textbody = stNr                                     '邮件内容
If Len(stFj) > 0 Then vCDO.AddAttachment stFj            '邮件附件

With vCDO.Configuration.Fields
    .Item(stUl & "smtpserver") = "oa.163.com"       'SMTP服务器地址
    .Item(stUl & "smtpserverport") = 25                  'SMTP服务器端口
    .Item(stUl & "sendusing") = 25                        '发送端口
    .Item(stUl & "smtpauthenticate") = 1                 '
    .Item(stUl & "sendusername") = stRx                 '发送方邮箱名称
    .Item(stUl & "sendpassword") = "zhangyl"                  '发送方邮箱密码
    .Update
End With
vCDO.Send                                                '发送
Set vCDO = Nothing
MsgBox "发送成功!", vbInformation, "提示"
End Sub

这段代码可以发邮件,但不能带附件,要怎么修改代码才可以带附件呢?请高手指点!

回复

使用道具 举报

发表于 2008-8-27 20:28 | 显示全部楼层

代码好长
回复

使用道具 举报

 楼主| 发表于 2008-8-27 23:27 | 显示全部楼层

那要怎么写?请指点!

回复

使用道具 举报

 楼主| 发表于 2008-8-27 23:28 | 显示全部楼层

没人知道吗?

回复

使用道具 举报

 楼主| 发表于 2008-8-28 00:52 | 显示全部楼层

Set   oApp   =   Outlook.Application   

会报错:编译错误 缺少函数或变量

这要怎么处理呀!谢谢了!

回复

使用道具 举报

发表于 2008-12-14 12:22 | 显示全部楼层

学习学习
回复

使用道具 举报

发表于 2012-4-14 16:37 | 显示全部楼层
因为你没有加引用》,要在工程里面引用outlook的库
QQ截图20120414163738.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2025-8-16 05:09 , Processed in 0.329276 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表