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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the Outlook session when sending an e-mail

Status
Not open for further replies.

boligoma

IS-IT--Management
Feb 11, 2003
31
MX
Hi,
I need to change the outlook session when sending an e-mail using VBA from Excel. The main point instead of sending the e-mail from the session "me@office.com" it send it from another account: "myoffice@office.com".

The only thing rigth now is the created e-mail with a string with the e-mail I want to send the message to. here is my mini sub:

Sub Contacts_in_mail(emailist As String)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = ""
.CC = ""
.BCC = emailist
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Thnks...
 
Hi,

Thank you for your responses. I've tried to use both properties (SendOnBehalfOf and MailLogon) but none works with the olMailItem. I don't know much about the mail properties from VBA (in fact I am not that savvy in Outlook at all). Do you have like an example in how to use these methods?.

I can use other method if it works, I just think this is the simplest one.

 

Using F1 :Logon Method
Code:
Sub StartOutlook()
 Dim myOlApp As Outlook.Application
 Dim myNameSpace As Outlook.NameSpace
 Set myOlApp = CreateObject("Outlook.Application")
 Set myNameSpace = myOlApp.GetNamespace("MAPI")
 myNameSpace.Logon "LatestProfile", , True, True
End Sub
 

The OMG makes this rather awkward. There isn't direct programmatic access to Accounts and you have to use the menus in the Mail Item's Inspector. Something like this (Outlook 2003) ...

Code:
[blue]Dim OutInspector As Outlook.Inspector
Dim OutAccounts As Office.CommandBarPopup
Dim OutAccount As Office.CommandBarButton 
Dim OutAccountToUse As String 
OutAccountToUse = "Tony @ MVPS" ' Put your Account Name here

Set OutInspector = OutMail.GetInspector
Set OutAccounts = OutInspector.CommandBars.FindControl(ID:=31224)
For Each OutAccount In OutAccounts.Controls 
    If Right$(OutAccount.Caption, Len(OutAccountToUse)) = OutAccountToUse Then 
        OutInspector.Activate 
        OutAccount.Execute 
    End If 
Next[/blue]

A point to watch out for: the programmatic view of the Accounts is not exactly the same as the UI view so ...
(a) after you have run the code the current account will not appear (to your code) to have changed, and
(b) deleted accounts may appear in the list before the current account

Also, it is slightly different - the menus are different - in Outlook 2000.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top