Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    Chapel Hill, North Carolina, USA
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom button to add name to 'To' line (2000 SR-1)

    I would like to add a button to the toolbar to automatically add a name to the "To" line of an email. I send many, many emails to a small number of folks and would like a faster way to select their names. I'm a VBA programmer in Excel & Access, but haven't been able to get my arms around the syntax. Any ideas?

    Thanks!

    Larry

  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: Custom button to add name to 'To' line (2000 SR-1)

    First let me assume you are not using WordMail. This all may be irrrelevant for WordMail.

    The message composition window is called an Inspector in the Outlook object model, and the one that is active is called, not surprisingly, the ActiveInspector. The message you are composing in the ActiveInspector is the CurrentItem. If the CurrentItem is a MailItem, then you can use the Add method of its Recipients collection to add a name to the "To" box. The code would look more or less like this:

    <pre>Sub MailJoe()
    AddToRecipient "Joe.Blow@domain.com"
    End Sub

    Sub AddToRecipient(strWho As String)
    Dim itmMessage As MailItem, strFingerprint As String
    'if macro is run in the wrong context, there will be an error
    On Error Resume Next
    Set itmMessage = ActiveInspector.CurrentItem
    If Err <> 0 Then
    MsgBox "Please create a new mail message and then try again. [" & _
    Err.Description & "]"
    Exit Sub
    End If
    On Error GoTo 0

    'add recipient
    With itmMessage.Recipients
    .Add strWho
    .ResolveAll
    End With
    Set itmMessage = Nothing
    End Sub</pre>

    I have separated a little short person-specific macro from the general "add a person" macro so that you don't have to repeat all that code over and over. The macro also will work with the person's name, as it appears in your address book, rather than the e-mail address, if the name is unique enough to avoid confusion for Outlook.

    Now... the bad news is that if you have installed SP2 or the subsequent security patch, the Object Model Guard will throw up permission dialogs on the .Add and .ResolveAll lines. Quite annoying. So hopefully you didn't. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  3. #3
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    Chapel Hill, North Carolina, USA
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Custom button to add name to 'To' line (2000 SR-1)

    Wow - too cool - Thanks - it worked like a charm!!! <img src=/S/bananas.gif border=0 alt=bananas width=33 height=35> Final question - to add a name to the CC line appears to require the use of ".Type", but again the syntax is killing me - is this easy to add?

    Larry

  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: Custom button to add name to 'To' line (2000 SR-1)

    I joke that I no longer do software development for money because as soon as you hit the nail on the head, expectations go up. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Anyway, you need an object reference to the new recipient in order to manipulate it. Yes, you could work through the index to the Recipients collection instead, but this is fast and precise and it's not much more code:

    <pre>'add recipient
    Dim recip As Recipient
    With itmMessage.Recipients
    Set recip = .Add(strWho)
    recip.Type = olCC
    .ResolveAll
    End With
    Set recip = Nothing
    Set itmMessage = Nothing</pre>

    If you swap that in for the last part above, it should take you where you need to go.

Posting Permissions

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