Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question on running Outlook from VBA to send emails

Status
Not open for further replies.

lxn

Programmer
Jan 29, 2002
53
CA
Hi, all.
okay, this is what I'm trying to do. I've already written the code where the application picks out certain people's ID and find their Email IDs, and then it will run Outlook from VBA and send email to each person selected. I've got all of that to work. But I have one question. I was wondering if there's a way that I can code it, so when the emails are sent, a more general name (such as "Shipping Services") appear in the 'From' field of the email instead of the email ID or address of the sender. The following is how I run outlook to send emails:

'start outlook application:
Set lobjOutlook = CreateObjec("Outlook.application")
'create new mail item (0):
Set lobjMail = lobjOutlook.createitem(0)
Let lobjMail.subject = lstrSubject
Let lobjMail.HTMLBody = lstrBody 'add body
Let lobjMail.to = lstrTo 'apply To list

''(the next two lines are not needed in this case)
'Let objMail.cc = lstrCc 'apply CC list
'Let lobjMail.bcc = lstrBCC 'apply bcc list

lobjMail.send
lobjOutlook.Quit

I was wondering if there's any method like ' .from' or some sort, that will allow me to change the From Field, like it does to the To Field and CC Field. (apparently lobjMail.from method does not exist :( )

if anyone knows how to make this work, PLEASE HELP ME?!!!?

Thanks a lot

 
This one is from Microsoft support article: Q232309

To set the From field, use the SentOnBehalfOfName property of the Item object. The following example demonstrates the use of the SentOnBehalfOfName property:
Sub TestSentOnBehalfOfName()

Dim myOlApp as Outlook.Application
Dim myItem as Outlook.MailItem

' Create an Outlook application object
Set myOlApp = New Outlook.Application

' Creates a new MailItem form
Set myItem = myOlApp.CreateItem(olMailItem)

' Set the "From" field
myItem.SentOnBehalfOfName = "Jon Grande"

' Display the item
myItem.Display

End Sub

Hope this helps,
Shannon
 
wow, this looks great, I'll try that out. Thanks a lot!
 
Hi again, I just tried what you suggested, and it worked out perfectly. Thanks a lot for your help.
 
Hi everybody,

Does anyone know why I have the following problem ?
I am using the routine as per above, and it works great, but the text of the email, when received in Outlook, is all jammed together.
I have used CHR(13) & chr(10) to add breaks lines to the message, and on a text box looks just fine, however on the email, is received without the break lines.

Also, the new Outlook security feature causes a message to pop up, requesting the user if we wish to let Access use Outlook to send an email.
Is there a way of saying to Outlook, "with Access program, let it send without warning".

Your help in this matter is appreciated.
regards
 
um, as to your first question, it's because you used mail.HTMLBody, so the line feed won't work in there. If you try changing it to mail.body instead, it should work out fine.

as to the second question, ppl has asked about it before, it's the sercurity thing that only happens with the latest Outlook security Patch, I think, it never happens to me. And most ppl I asked say there's not much you can do except maybe only outgoing email and not install the security patch. Don't know how that's gonna work out.

Merlin
 
thanks lnx

mail.body works perfectly !!!!!

No problem about the security feature. I am creating a database for our company and we cannot disable the security feature for obvious reasons.

Hopefully a way around it will come soon from Microsoft (yeah, right).
regards
Frank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top