Tuesday, November 2, 2010

Automating Outlook 2011 with Applescript

I am continuing my automation work with folder actions and applescript and had a desire to be able to email from the new Mac Office 2011 using Outlook. There are several examples for how to do it with Entourage but the syntax has changed regarding mail to recipients. Without further delay, here is a working script:

tell application "Microsoft Outlook"
 set newMessage to make new outgoing message with properties {subject:"Hooray for automation"}
 make new recipient at newMessage with properties {email address:{name:"Jim Shank", address:"jim.shank@example.com"}}
 open newMessage
end tell

After hours of searching, I finally came across this fantastic example from Bobby Kansara at Microsoft. Thanks to the early adopters and beta testers.

13 comments:

  1. Nice - thanks. I was testing with SEND instead of Open and it just puts the message in the drafts folder. Seems like a bug to me.

    ReplyDelete
  2. I agree, it does seem bugged. I always need to add a bit of text before sending the message so hadn't tried a fully automated send yet.

    ReplyDelete
  3. I am trying to add bcc to an existing window before sending. I had a script in Entourage that used to work by pressing ^B, but the same does not work here. If I use your script I can create a new window with bcc, but I am unable to set the current window to add bcc, if you can help it would be great ...

    ReplyDelete
  4. I'll give it a try and see if mine won't send.

    ReplyDelete
  5. How do you get Outlook to send?? This script creates a message but I'd like to automatically send, too.

    Thanks!

    ReplyDelete
  6. The docs say "send newMessage". Can anyone confirm?

    ReplyDelete
  7. Changes in the Applescript dictionary from Entourage to Outlook has messed me up, too. I HAD a script which parses a list of items such as name, project, email and name of an attachment file and created outgoing messages in the draft box (as it was called). After looking them over for correctness, I selected and dragged them into the outbox and away they went.

    I am getting type errors that I cannot seem to fix or even isolate. My working Entourage make message line was:
    make new outgoing message at draftbox with properties {recipient:theRecipient, subject:theSubject, content:messageBody, has html:true, attachment:theFilePath}

    which I changed to:
    make new outgoing message at drafts with properties {recipient:theRecipient, subject:theSubject, content:messageBody, has html:true, attachment:theFilePath}

    and am getting the error:
    Can’t make "email@email.com" into type recipient.

    In trying to break the statement apart, I've tried a number of iterations. The latest may give someone a clue what's going on that I'm not seeing:

    set theAccount to pop account "Evaluator"
    tell drafts of theAccount
    set theMsg to make new outgoing message with properties {subject:"subject", content:"body", account:theAccount}
    end tell
    make new attachment at theMsg with properties {name:theFilePath}
    make new content at theMsg with properties {content:messageBody, has html:true}
    make new to recipient at theMsg with properties {email address:{name:FirstName, address:theRecipient}, type:to recipient type}
    send theMsg

    First 5 lines execute fine. The next line fails with:
    Can’t make missing value into type location specifier.

    Interestingly, any of the 3 'make' lines get the same error, when I reorder them to execute as line 6. That doesn't make sense to me, but maybe to someone reading this?

    ReplyDelete
  8. Actually, I can't even get this to work:
    set theAccount to pop account "Evaluator"
    tell drafts of theAccount
    set theMsg to make new outgoing message with properties {subject:"subject", content:"body", account:theAccount}
    end tell
    send theMsg

    I get:
    Can’t make missing value into type message.

    ReplyDelete
  9. This script doesn't work in Lion 10.7.2. I get the AppleScript error:
    Microsoft Outlook got an error: Can’t make class outgoing message.
    Something's become broken there?

    ReplyDelete
  10. I can't believe it! I got Microsoft Office to send!

    Specs are: MacOS 10.7.3, Microsoft Outlook for Mac 14.0.0 connected to an Exchange server.

    Code is:
    tell application "Microsoft Outlook"
    set OutgoingEmail to make new outgoing message with properties {subject:"subject", content:"body"}
    make new recipient at OutgoingEmail with properties {email address:{name:"", address:"me@test.com"}}
    send message id (id of OutgoingEmail)
    end tell

    Note: Sometimes the email sends but it also stays in Drafts. It seemed to only happen during testing. Haven't used this enough to know if it happens during production.

    Rob Stokes

    ReplyDelete
  11. Outlook seems to be ignoring returns in the text string of my message content
    E.g,
    set myContent to "line one" & return & "line two" as string
    ...
    set OutgoingEmail to make new outgoing message with properties {subject:”subject”, content:myContent}

    the resulting email message content is line oneline two (running the two paragraphs into one).
    Ideas?
    Thanks

    ReplyDelete
  12. Hi Jim, I'm wondering if you've figured out how to get "Split Recipients" applescript working in Outlook 2011? I tried it and get an error, but don't know code well enough to figure out what needs changing. I can send you the applescript file if you like. Many thanks.

    ReplyDelete
  13. It proved to be Very helpful to me and I am sure to all the commentators here! msn hotmail

    ReplyDelete