More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 24-May-2018 19:25

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Outlook Expert Techniques Printer Friendly Version
Title: Setting the From (sender) on a message
Description: Outlook provides no direct way to set the From value for all messages. If the user has Outlook 2002/3 and is not using WordMail as the editor, however, you can use the code below to set the sending account using CommandBars techniques.
Date: 13-May-2005  03:57
Code level: intermediate
Code area: Outlook Expert Techniques
Posted by: Sue Mosher
This message is displayed as VB.NET
 Function Set_Account(ByVal AccountName As String, M As Outlook.MailItem) As String
    Dim OLI As Outlook.Inspector
    Dim strAccountBtnName As String
    Dim intLoc As Integer
    Const ID_ACCOUNTS = 31224

    Dim CBs As Office.CommandBars
    Dim CBP As Office.CommandBarPopup
    Dim MC As Office.CommandBarControl

    Set OLI = M.GetInspector
    If Not OLI Is Nothing Then
        Set CBs = OLI.CommandBars
        Set CBP = CBs.FindControl(, ID_ACCOUNTS)
        If Not CBP Is Nothing Then
            For Each MC In CBP.Controls
                intLoc = InStr(MC.Caption, " ")
                If intLoc > 0 Then
                    strAccountBtnName = Mid(MC.Caption, intLoc + 1)
                Else
                    strAccountBtnName = MC.Caption
                End If
                If strAccountBtnName = AccountName Then
                    MC.Execute
                    Set_Account = AccountName
                    GoTo Exit_Function
                End If
            Next
        End If
    End If
    Set_Account = ""

Exit_Function:
    Set MC = Nothing
    Set CBP = Nothing
    Set CBs = Nothing
    Set OLI = Nothing
End Function 
All 68comments
Page [ 1 2 3 4 5 6 7 Next >>  
  13-May-2005  11:01   
Other methods:

1) In an Exchange environment where you want to use another user's mailbox as the From address, set the MailItem.SentOnBehalfOfName property. If you do not want the current user's name to appear in the message at all, you must grant Send As permission on the other user's mailbox.

2) Use the Redemption library to set an RFC822 header property, as described at http://www.dimastr.com/redemption/faq.htm#14

3) If you're more concerned about replies to your message going to the correct place, add the desired reply address to the MailItem.ReplyRecipients collection.
  13-Sep-2005  10:10   
Sue:

I had been looking for a way to change the account when creating a new message and this looks like it, but it doesn't work for me either. I must admit that I am new to VBA and Office programming, but I have coded for years. I have Outlook 2003.

All I want to do is create a button on my Outlook Today page to open a new mail message for each account. As a simple test before inserting it into my HTML page, I created a new module, inserted your simplified function, and wrote this simple subroutine to open the new message. The new message is created with "Comcast" in the Subject, but the account is still my default account. This task should not be that difficult so what am I doing wrong?

Sub NewComcastMessage()
    Dim objMsg As Outlook.MailItem
    Dim result As String
    
    'create new outgoing message
    Set objMsg = Application.CreateItem(olMailItem)
    'select the account
    result = Set_Account("Comcast", objMsg)
    objMsg.Subject = result
    'open the message window
    objMsg.Display
    Set objMsg = Nothing
End Sub

My little project to customize my Outlook Today page has made me want to purchase your book. I have had to fumble around trying to figure out how do run commands and macros from an HTML page. Fortunately the heavy lifting was already done on the default pages. I just added new links and extended the functionality of what was already there. Now I would like to go on and automate several common tasks. Your site has been a tremendous help.

Thanks,
Mark Milliman
  13-Sep-2005  10:27   
Sue:

I assume that the function will show that the account was changed. That is if I click on the "Account" button, I will see "Comcast" as the account instead of the default. Also a status message will be just below the toolbars in OLK2003.

My test message confirmed that the account was still my default account. Why does MS make it so hard to change the account? Apparently they want users to manipulate accounts or why would they allow the setting a signature for each account in the Mail Format dialog.

Mark
 
  13-Sep-2005  10:47   
Sue:

Sorry, but I am trying to give your more information to identify the problem. The fact that I have selected stationary and a signature with HTML formatting should have no impact right? I am not using Word as an editor either.

Mark
  13-Sep-2005  13:22   
All the code sample does is execute the toolbar command. The message should be displayed *before* you attempt to change the code. Otherwise, the toolbar won't be available.
  13-Sep-2005  21:50   
THANKS SUE! Duh! I guess I did not understand that working with the toolbars is like running a macro that recorded keystrokes. So now I know that any time I use this procedure that I need to have an Explorer or Inspector window open. It works perfectly. Now all I need to do is add the macros to my Outlook Today page.

Mark
  14-Sep-2005  04:42   
Okay now that I got the code working, I wanted to call it from my Outlook Today page. I added 3 lines of VBScript to the HTML file to call my subroutine that calls this function. It worked only when I had VBA initialized; otherwise, I received an error message. Microsoft says by default, Outlook Visual Basic for Applications is not initialized, for performance reasons, when you start Outlook. This means that if you start Outlook, open an item, and run the VBScript code, it will produce an error because Visual Basic for Applications is not available at that time.

My alternative is to convert the function to VBScript. I don't know all of the differences between writing VBA and VBScript. Where can you point me so I know how to change this function? I may was well execute the whole thing from the HTML page. This function will work using VBScript right? Of course, because that is how it will end up in a form.

Thanks again,
Mark
  14-Sep-2005  07:49   
The main differences between VBA/VB and VBSCript is that VBScript has :

-- no data typing of variable or procedure declarations. You would need to delete or comment out all the As clauses.

-- no knowledge of any constants other than its own (e.g. vbCrLf). You would need to declare any Outlook constants that your code uses or use their literal values, which you can look up in the object browser.

The only way to know whether it will work from VBScript is to try it. Let us know how it goes.
  14-Sep-2005  12:02   
Sue:

Thanks for your pointers. I used the VBScript reference from MSDN too to modify my code. It all seems straight forward, but somehow I have a syntax error in it. I cannot pinpoint it because FrontPage does not point to the right spot. Can you see any problems with it?

Function Set_Account(AccountName, M)
    Dim OLI 'As Outlook.Inspector
    Dim strAccountBtnName 'As String
    Dim intLoc 'As Integer
' Const ID_ACCOUNTS = 31224

    Dim CBs 'As Office.CommandBars
    Dim CBP 'As Office.CommandBarPopup
    Dim MC 'As Office.CommandBarControl

    Set OLI = M.GetInspector
    If Not OLI Is Nothing Then
        Set CBs = OLI.CommandBars
        Set CBP = CBs.FindControl(, 31224)
        If Not CBP Is Nothing Then
            For Each MC In CBP.Controls
                intLoc = InStr(MC.Caption, " ")
                If intLoc > 0 Then
                    strAccountBtnName = Mid(MC.Caption, intLoc + 1)
                Else
                    strAccountBtnName = MC.Caption
                End If
                If strAccountBtnName = AccountName Then
                    MC.Execute
                    Set_Account = AccountName
                    GoTo Exit_Function
                End If
            Next
        End If
    End If
    Set_Account = ""

Exit_Function:
    Set MC = Nothing
    Set CBP = Nothing
    Set CBs = Nothing
    Set OLI = Nothing
End Function

Mark
  21-Nov-2005  12:34   
Hello Sue, thank you very much for this code !
Why this code does not Work twice of continuation or if i have select manually the account before ?
Page [ 1 2 3 4 5 6 7 Next >>