简体   繁体   English

VBA 从当前用户地址发送带有 CDO 的电子邮件

[英]VBA send Email with CDO from the current user adress

I try to send a email with a vba script that uses CDO and Outlook.我尝试使用使用 CDO 和 Outlook 的 vba 脚本发送电子邮件。

I use the script you can find here : http://www.cpearson.com/excel/Email.aspx我使用您可以在这里找到的脚本: http : //www.cpearson.com/excel/Email.aspx

It works fine, but I want to send email from the Outlook account of the person using the vba.它工作正常,但我想从使用 vba 的人的 Outlook 帐户发送电子邮件。 How I can get the email adress of the current user ?如何获取当前用户的电子邮件地址?

easiest way to achieve this is to delete all lines that wait for the "fromadress" in thee code from cpearson.实现此目的的最简单方法是删除所有等待 cpearson 代码中“fromadress”的行。 If you delete the following lines:如果删除以下几行:

If Len(Trim(FromAddress)) = 0 Then
    SendEMail = False
    Exit Function
End If

and

.From = FromAddress

and change the Definition of the function like this:并像这样更改函数的定义:

Function SendEMail(Subject As String, _
        ToAddress As String, _
        MailBody As String, _
        SMTP_Server As String, _
        BodyFileName As String, _
        Optional Attachments As Variant = Empty) As Boolean

then no fromaddress is set and automaticalyy the standard-address of Outlook will be used.然后没有设置 fromaddress 并自动使用 Outlook 的标准地址。

It's impossible with CDO because .from is a required parameter. CDO 不可能,因为 .from 是必需参数。

The solution is to use native Outlook VBA (course you should want to send your mail from Outlook not GMAIL or other).解决方案是使用本机 Outlook VBA(当然,您应该希望从 Outlook 而不是 GMAIL 或其他发送邮件)。 There is a lot of example here :这里有很多例子:

http://www.rondebruin.nl/win/s1/outlook/amail1.htm http://www.rondebruin.nl/win/s1/outlook/amail1.htm

http://www.rondebruin.nl/win/s1/outlook/mail.htm http://www.rondebruin.nl/win/s1/outlook/mail.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM