Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Dallas, Texas, USA
    Posts
    1,680
    Thanks
    0
    Thanked 1 Time in 1 Post

    vba to set 'account' (Outlook 2007)

    I use this code to set up toolbar buttons for quickly forwarding mail to others. It works great.
    But...
    I need to add to the code so that I can forward the mail from a "specific" email account, rather than the default. I've researched this and I think I need to add a line like
    .SenderName = "tricky@gmail.com"
    But that does not seem to work; resulting in an error message.

    How can I set the account from which to forward the emails?


    <pre>Sub Fwd_To_Home()
    Dim item As Object
    Dim itmFwd As Object
    Dim recipMe As Recipient
    On Error Resume Next
    If TypeName(Application.ActiveWindow) = "Explorer" Then
    Set item = ActiveExplorer.Selection(1)
    Else
    Set item = ActiveInspector.CurrentItem
    End If
    On Error GoTo 0
    If Not item Is Nothing Then
    Set itmFwd = item.Forward
    With itmFwd
    Set recipMe = .Recipients.Add("home@gmail.com")
    recipMe.Resolve
    .Save
    <font color=red> ' The following line was added, but does not work as I thought it would
    .SenderName = "tricky@gmail.com"</font color=red>
    .Send
    End With
    Else
    MsgBox "Nothing selected"
    End If
    Set item = Nothing
    End Sub</pre>

    - Ricky

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: vba to set 'account' (Outlook 2007)

    Lucky you! Microsoft has finally made this (relatively) easy in Outlook 2007. You can integrate this with your code, or keep it as a separate sub you call:

    <code>Sub SetSendingAcct(ByRef mi As Outlook.MailItem, strAddress As String)
    Dim acct As Outlook.Account
    For Each acct In Session.Accounts
    If acct.SmtpAddress = strAddress Then
    mi.SendUsingAccount = acct
    mi.Save ' This makes the change visible in the UI
    Exit For
    End If
    Next
    End Sub

    Sub TESTSetSendingAcct()
    SetSendingAcct ActiveInspector.CurrentItem, "tricky@gmail.com"
    End Sub</code>

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Dallas, Texas, USA
    Posts
    1,680
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: vba to set 'account' (Outlook 2007)

    Hey Jefferson - Thanks for the help. You almost have to draw this out with a crayon for me... <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Where in my block of code would I place the "call"?

    I already have a toolbar button that works - After selecting a message in the inbox, I just click the macro button, and the message is quickly forwarded to a "special person". What I am trying to accomplish now, is making certain that the button uses a specific email account (example: tricky@gmail.com). That is not my default account, it's just the one I'd want used for that particular forwarding button.

    I find myself forwarding a bunch of messages; selecting and clicking a button on the toolbar certainly quickens things up a bit.

    Is this the way it should look?

    <pre>Sub SetSendingAcct(ByRef mi As Outlook.MailItem, strAddress As String)
    Dim acct As Outlook.Account
    For Each acct In Session.Accounts
    If acct.SmtpAddress = strAddress Then
    mi.SendUsingAccount = acct
    mi.Save ' This makes the change visible in the UI
    Exit For
    End If
    Next
    End Sub
    ________________________________________________
    Sub TESTSetSendingAcct()
    SetSendingAcct ActiveInspector.CurrentItem, "tricky@gmail.com"
    End Sub
    ________________________________________________
    Sub Fwd_To_SpecialPerson()
    Dim item As Object
    Dim itmFwd As Object
    Dim recipMe As Recipient
    On Error Resume Next
    If TypeName(Application.ActiveWindow) = "Explorer" Then
    Set item = ActiveExplorer.Selection(1)
    Else
    Set item = ActiveInspector.CurrentItem
    End If
    On Error GoTo 0
    If Not item Is Nothing Then
    Set itmFwd = item.Forward
    <font color=red>Call SetSendingAcct
    Exit</font color=red>
    With itmFwd
    Set recipMe = .Recipients.Add("SpecialPerson@gmail.com")
    recipMe.Resolve
    .Save
    .Send
    End With
    Else
    MsgBox "Nothing selected"
    End If
    Set item = Nothing
    End Sub</pre>


    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    - Ricky

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: vba to set 'account' (Outlook 2007)

    The TEST procedure is your model on how to call the SetSendingAcct procedure. Where you have the red text, try this instead:

    <code>SetSendingAcct itmFwd, "tricky@gmail.com"</code>

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Dallas, Texas, USA
    Posts
    1,680
    Thanks
    0
    Thanked 1 Time in 1 Post

    Thank you.

    That works great. Thanks a bunch.
    - Ricky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •