0

I try to send a email with a vba script that uses CDO and Outlook.

I use the script you can find here : 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. How I can get the email adress of the current user ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Slek33
  • 1
  • 1
  • 5
  • Using that code you can't, unless you have a way to map your usernames to email addresses. Try using the native Outlook VBA such as here - http://support.microsoft.com/kb/161088 – enderland May 28 '14 at 13:10
  • Are you trying to use the email address associated with Outlook profile setup on the PC the user is using? If so, then [this may help you](http://stackoverflow.com/questions/17883088/excel-vba-sending-mail-using-outlook-send-method-fails). If you are not wanting to use the email address from the configure Outlook profile on the PC (or there isn't one), then you will need to figure out if there is a way to catch this through their username or possibly from Active Directory. – Adach1979 May 28 '14 at 13:28
  • @enderland you are right. Finally I used native outlook VBA such as here : http://www.rondebruin.nl/win/s1/outlook/amail1.htm – Slek33 Jun 02 '14 at 08:12

2 Answers2

0

easiest way to achieve this is to delete all lines that wait for the "fromadress" in thee code from cpearson. 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.

Max
  • 744
  • 1
  • 7
  • 19
  • The from parameter is required. If I try to send mail without him I get the following error : 'code'"Runtime error -2147220979(8004020d) At least one of the froms or sender fields is required"'code' – Slek33 Jun 02 '14 at 06:53
  • you markes this as answer but your comment suggests it does not work - to you need any further help? – Max Jun 04 '14 at 13:41
  • Excuse me, I tick the wrong answer, the solution is my answer below. – Slek33 Jun 06 '14 at 11:42
0

It's impossible with CDO because .from is a required parameter.

The solution is to use native Outlook VBA (course you should want to send your mail from Outlook not GMAIL or other). There is a lot of example here :

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

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

Slek33
  • 1
  • 1
  • 5