Link to home
Start Free TrialLog in
Avatar of Sar1973
Sar1973Flag for Italy

asked on

Send email with VBA - how to select/load the account?

Hi I have a simple VBA code that allows me to send emails using a MS Access DB.
Could you post a few lines that make possible to previously load in Oulook the email account that I would like to use for these transmission? Thanx.
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Sar1973,

I don't know if this is possible, because you might have to enter a password.

Pleaes explain, in dertail, what you mean by:
<load in Oulook the email account that I would like to use for these transmission>

JeffCoachman
Avatar of Sar1973

ASKER

No passwords. I have say 2 account in my Outlook 2000: I want to choose from which one the email should be sent.
I also want to know if it is possible to inidicate the name of the recipient with its email address (say FakeAddress@unreal.com "DR.SAR"), so that in the "To" field the recipient may see its name and not only its email address. Thanx.



{08-Sep-08:  email address edited for spam prevention -- mbizup, Access Zone Advisor/Page Editor}
Sar1973,

You might want to add the Exchange Zone to this question.
(Click the "Request Assistance" button in your original post)

If you could specify the account (Which again, I am not sure of), you would have to make sure that the Recipient HAD a "real" name in addition to an email address.
(Outlook will uses the real name by default)

JeffCoachman
Avatar of Sar1973

ASKER

I am still waiting for an answer; definitely, I am an Outlook 2000 user that wants to select a sender account alternative to the default when creating with VBA an email message.
The ".from=email@provider.it" VBA command does not work; I have to select the account manually in the email preview, clicking on the "From:" button or typing it in the beside space.
Thanx.
Vee,
Can you add the Exchange Zone?
Jeff
Sar1973,

This function, using CDO allows me to set the "FROM" address, assuming that it is already set up in Outlook.

One of the nice things about using CDO is that you don't get the warning messages that you get sending emails using outlook objects (although you might not get these warnings in OL 2000 anyhow), and you have the same options.

Call the function like this if you have a specific From address that you want to use:
cdosendemail "MyFromAddress@MyEmail.com","YourToAddress@YourEmail.com", "Interesting subject", "Totally Brilliant Message"

The function also lets you use a default From address by using NULL or empty string ("") in the varFrom argument:
cdosendemail "","YourToAddress@YourEmail.com", "Interesting subject", "Totally Brilliant Message"
cdosendemail Null, YourToAddress@YourEmail.com", "Interesting subject", "Totally Brilliant Message"








Function CDOSendEmail(varFrom As Variant, strTo As String, strSubj As String, strMess As String)
    Dim strDefaultFrom  As String
    Dim objMess As Object
    Set objMess = CreateObject("CDO.Message")
      
    ' This is your default FROM address, and to be used if varFrom is Null or Empty
    strDefaultFrom = "DefaultAddress@MyEmail.com"
    
    ' Subject
    objMess.Subject = strSubj
    
    ' Message
    objMess.textbody = strMess
    
    ' Determine if varFrom is Null/Empty and use default address,
    ' otherwise use the From address passed through the function call
    objMess.From = IIf(Nz(varFrom, "") = "", strDefaultFrom, varFrom)
    
    ' To
    objMess.To = strTo
    
    ' This is a simple example... you can do a lot more
    
    ' Send it
    objMess.Send
    
    ' Clear the object variable
    Set objMess = Nothing
    
End Function
 

Open in new window

LOL,

Yes it seems that the "mbizup Zone" has already been added to this Q!
;-)

Jeff
Avatar of Sar1973

ASKER

Sorry, I get an error on "objMess.Send": it says "Property not supported", as when I try to launch the ".From=..." command with the other code. The real problem is that if I create an OL2000 model, I set by default the sender address and I try to save it in order to use that address on each message I create, it does not save the sender address in the .oft file. Any idea...?
Avatar of Sar1973

ASKER

Maybe it is possible with HTML, doing something like:
olkmsg.HTMLbody = olkmsgHTMLbody & "<B>From:</B>address@email.it<BR><B>"
Sar1973,

>I get an error on "objMess.Send": it says "Property not supported", as when I try to launch the ".From...
>Any idea...?

Nothing sound :-(

The code I posted works on this end, so it sounds like a configuration difference.

Some thoughts:

- Did the code fail for you exactly as I posted it, or was it a problem with integrating it into your own code?
- Are there any security settings that might be preventing this from working (for example, I can't use CDO to programmatically send emails from my work computer)
- This function works for me using Office 2007 and 2003, but I don't have 2000 (it's entirely possible, as Jeff suggested, that this will not work with OL 2000).
- Can you test these functions on any other computers to isolate configuration differences as problems?


Also -- can you post your original email function here (full code)?



Avatar of Sar1973

ASKER

-I posted it exactly as you did
-No
-I guess that's the problem: I asked for OL2000
-Same trouble

Public Function PrepareEmail(...)
 
    Dim olkApp As Object, olkMsg As Object
 
    Set olkApp = CreateObject("Outlook.application")
    Set olkMsg = olkApp.CreateItemFromTemplate(myModel)
 
    With olkMsg
            .ReadReceiptRequested = True
            .To = oIndirizzo
            .HTMLBody = oHTML
            .Attachments.Add myAttach
            .Save
    End With
 
    Set olkApp = Nothing
    Set olkMsg = Nothing
 
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sar1973

ASKER

I will: in the meanwhile can you tell me if the HTML method can be used instead?
Avatar of Sar1973

ASKER

I have tried to select CDO library and create the message: no way. I am really thinking of developing 1 or 2 lines of code useful to add HTML body which configures the sender.
After the first attempts, I have managed only to add "From: adress@email.it" in the body of the email, not in the head. Any idea...?
Sar1973,

I don't know how to make the HTML Body idea work.

> I have tried to select CDO library and create the message: no way.

Can you be more specific (error messages, missing library, etc)?

I will try to send other Experts this way.

That error is in a different block of code.

> I have tried to select CDO library and create the message: no way.
My question concerned what the specific problem was using CDO in your last test in *this* thread.

bluedevilfan's approach very well might work with OL 2000.

EE allows a maximum of 500 points per question. Since you are receiving help on a duplicate question, you should request that this one be deleted (the Delete button in your original post).

Also consider VM's comments in your recent Community Support thread.
https://www.experts-exchange.com/questions/23711599/10-Sep-Automated-Request-for-Attention-Q-23518737.html#22419554 
Since you have unlimited points, raising the points in your duplicate question can't hurt. As both of these threads are showing, getting this working using Outlook 2000 is not a trivial issue.


Avatar of Sar1973

ASKER

Just explain how to raise the points assigned to this question, or do it yourself.
Avatar of Sar1973

ASKER

Would the .Account property be useful to the scope?
Sar1973,

The conclusion I've come to here is the same as what BlueDevilFan has stated in your other question.  This type of code works fine with Outlook 2003 and 2007, but there is no way to do this directly in Outlook 2000.

Please see the following thread, including the comment and link provided by Sue Mosher (Outlook MVP) at the very end:
http://www.devnewsgroups.net/group/microsoft.public.office.developer.outlook.vba/topic51978.aspx

The link that she has provided shows you how to accomplish this indirectly, by programmatically "clicking" the necessary command buttons and selecting the account (using Outlook's Inspector).

The code involved is very similar to what BlueDevilFan has already posted in your duplicate question.

The point of my previous comment:
    -  EE does not allow duplicate questions.
    - BlueDevilFan has already provided you with the basic method needed to solve this problem.
    - You should delete this question and continue to work with the other one.
Avatar of Sar1973

ASKER

I finally found it: .SentOnBehalfOfName = "address@email.it"
Avatar of Sar1973

ASKER

No solution, I found it elsewhere.
Sar1973,

I did suggest that you try "SentOnBehalfOfName" as a workaround in this earlier post:
http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_23518737.html#22445910

Have you tried sending a message like this to yourself?

The CDO method I posted and also the .From property in more recent versions of Outlook will only show the recipient the .From address that you specified.

The reason I called the "sentOnBehalfOfName" property a workaround is that the recipient sees both your actual address and the From address that you specify in code. Like this:

You [You@RealEmail.com]; on behalf of; YourSentonBehalfofName@OtherEmail.com
From your follow-up comments, I had assumed that was not an acceptable solution.


> No solution, I found it elsewhere.

At any rate, if that is the solution you are using, I did post it in this thread.





Avatar of Sar1973

ASKER

Maybe, between the lines.
From http:#a22445910 -

< The closest thing I found that was not read-only was the .SentOnBehalfOfName property. Give that a try - and see if it is close enough.>
No "reading between the lines" needed.