How to copy a file from a selected message to an outgoing message in outlook through applescript?

239 views Asked by At

I have been trying to figure out how to copy an attachment from an incoming message in outlook to a new outgoing message through applescript.

The code below is what I've got so far, but it's definitely wrong. Outlook throws and error at me stating: "Microsoft Outlook got an error: Can’t make missing value into type file."

tell application "Microsoft Outlook"

-- get the incoming message
set objMessage to item 1 of (get current messages)
set myMessage to make new outgoing message

-- iterate through attachments
-- for each attachment make a new attachment in the message to send identical properties
-- ???
repeat with _attachment in attachments of objMessage
    set n to name of _attachment
    set ct to content type of _attachment
    set f to file of _attachment
    set fs to file size of _attachment
    tell myMessage
        make new attachment with properties {name:n, content type:ct, file:f, file size:fs}
    end tell

end repeat

open myMessage


end tell

I've tried a couple of other things, but I can't seem to get at the file property of the incoming messages attachment.

Does anybody have any experience working with files in Outlook and Applescript in this way?

1

There are 1 answers

1
Frank C. On BEST ANSWER

Dylan

It appears that just passing the attachment between messages doesn't work but saving the attachment first and then passing the file name to the file property for the new attachment does.

tell application "Microsoft Outlook"
    local mynames, msg1, msg2, saveToFolder, saveAsName
    set msg1 to last item of incoming messages
    set msg2 to make new outgoing message with properties {subject:"Testing this thing"}
    set saveToFolder to POSIX path of (path to desktop)
    repeat with atts in attachments of msg1
        set saveAsName to saveToFolder & (name of atts)
        log saveAsName
        save atts in saveAsName
        tell msg2
            make new attachment with properties {file:saveAsName} -- with properties {file:atts}
        end tell
    end repeat
    open msg2
end tell

Note 1: You will probably want to add logic to remove the files (original attachments) that were saved from the incoming message.

Note 2: Another option would be to find the location of the file from the incoming message and just use that alias instead of saving them first just to add them to the outbound message'