vba 使用 .Vbs 文件打开默认电子邮件客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12268942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:34:59  来源:igfitidea点击:

Open Default E-mail Client using .Vbs file

vbavbscript

提问by user1513192

Possible Duplicate:
Send e-mail through VBA
Send email from Excel in Exchange environment

可能重复:
通过 VBA
发送电子邮件 在 Exchange 环境中从 Excel 发送电子邮件

I have this so far

到目前为止我有这个

Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
objMailItem.Display
strEmailAddr  = "[email protected]"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Body = "Hi"
objMailItem.Attachments.Add "access.xml"
Set objMailItem = nothing
Set objOutl = nothing

It works! But only on computers that have Outlook. How can I get this to work with computers that have Windows Live?

有用!但仅限于装有 Outlook 的计算机。我怎样才能让它与装有 Windows Live 的计算机一起工作?

回答by Jean-Fran?ois Corbett

Windows Live Mail (WLM) doesn't support automation via VBA, so it isn't as straightforward as with Outlook.

Windows Live Mail (WLM) 不支持通过 VBA 实现自动化,因此它不像 Outlook 那样简单。

For other options, try typing [vba] e-mailin the search field. You'll get quite a few hits; here is a relevant sample: Hit, hit, hit. Some of these give you working code for sending mail using CDO. This is what I would do if I were you.

对于其他选项,请尝试[vba] e-mail在搜索字段中输入。你会得到不少点击;这是一个相关示例:Hit, hit, hit。其中一些为您提供了使用 CDO 发送邮件的工作代码。如果我是你,这就是我会做的。

If you mustuse WLM, then have a look at this mail add-ins for Excelwhich does support WLM.

如果您必须使用 WLM,请查看支持 WLM 的Excel 邮件插件

Otherwise you're stuck using VBA's SendMailmethod, which is very limited:

否则,您将无法使用 VBA 的SendMail方法,这是非常有限的:

  • Can only send an Excel object such as a sheet, workbook, chart, range, etc.
  • Can't write text in the body of the e-mail
  • Can't use the CC or BCC fields
  • Can't attach files (other than the Excel object calling the method)
  • 只能发送 Excel 对象,例如工作表、工作簿、图表、范围等。
  • 无法在电子邮件正文中写入文本
  • 不能使用抄送或密件抄送字段
  • 无法附加文件(调用该方法的 Excel 对象除外)

Example code:

示例代码:

Dim wb As Workbook
Set wb = ActiveWorkbook
wb.SendMail "[email protected]", _
            "Insert subject here"

For more examples look here: http://www.rondebruin.nl/sendmail.htm

有关更多示例,请查看此处:http: //www.rondebruin.nl/sendmail.htm

回答by TheNewOne

the following suppose to work on access (vba) (code is not mine):

以下假设适用于访问(vba)(代码不是我的):

Public Function send_email()

Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") =   "[email protected]"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Update
End With
' build email parts With cdomsg
.To = "[email protected]"
.From = "[email protected]"
.Subject = "the email subject"
.TextBody = "the full message body goes here. you may want to create a variable to hold  the text"
.Send
End With
Set cdomsg = Nothing
End Function

note if you want to use other email service you should alter the code a bit.

请注意,如果您想使用其他电子邮件服务,您应该稍微修改一下代码。

some other options here - msdn reference

这里的一些其他选项 - msdn 参考

Hope it helps.

希望能帮助到你。