Jump to content

Problem with MS Outlook COM


Recommended Posts

I am trying to interact with microsoft outlook through the COM interface. I want to use the GetTemporaryFilePath (msdn reference here) method to get information about an attachment to an email. For some reason I am getting a blank string from this method instead of the path I want.

Here is my test code. It just creates a new email and adds an attachment and tries to get the temppath of it. (of course you will need to have outlook installed) (I am using outlook 2010):

;com error handler
$oMyError = ObjEvent("AutoIt.Error", "ErrFunc")

;create new email item and display
$oOutlook = ObjCreate("Outlook.Application")
$oMessage = $oOutlook.CreateItem(0)
$oMessage.display()

;register events for the new email item
$oevent = ObjEvent($oMessage,"oMessage_")

;event for when attachment is added
Func oMessage_AttachmentAdd($oAttachment)
    ConsoleWrite($oAttachment.filename & ' was added.' & @CR)
    ConsoleWrite('type=' & $oAttachment.type & @CR)
    ConsoleWrite('temp path=' & $oAttachment.GetTemporaryFilePath() & @CR)
EndFunc

;add an attachment
$oMessage.attachments.add("C:\windows\system32\calc.exe")

;keep script alive to receive events
while 1
    sleep(100)
WEnd

; This is a custom error handler
Func ErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(0, "", "We intercepted a COM Error !" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "WinDescription is: " & $oMyError.windescription)
    $iEventError = 1 ; Use to check when a COM Error occurs
EndFunc   ;==>ErrFunc

The output I get is:

calc.exe was added.
type=1
temp path=

I also get COM error 80020009 "Exception Occurred". Unfortunately I'm not finding anything helpful searching for this.

I tried the equivalent code in VBA in outlook and it works fine:

Public WithEvents newItem As Outlook.MailItem

Private Sub newItem_AttachmentAdd(ByVal newAttachment As Attachment)
    If newAttachment.Type = olByValue Then
        Debug.Print newAttachment.FileName & " was added."
        Debug.Print "type=" & newAttachment.Type
        Debug.Print "temp path=" & newAttachment.GetTemporaryFilePath
    End If
End Sub

Public Sub TestAttachAdd()
    Set newItem = ThisOutlookSession.CreateItem(olMailItem)
    newItem.Attachments.Add ("C:\windows\system32\calc.exe")
    newItem.Display
End Sub

Output is:

calc.exe was added.
type=1
temp path=C:\Users\<USERNAME>\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook\GF7OKDY4\calc.exe

Does anyone have any idea why GetTemporaryFilePath is not working in autoit or have any idea of anything I can try to get that information? Hopefully this is not just some limitation of autoit's COM interface?

Edited by garbb
Link to comment
Share on other sites

I tried your code, I have the same error.

Strange, GetTemporaryFilePath fails. However, the $oAttachment.type is 1 (olByValue), so it should work...

Everything seems to be ok ...

More informations for the error  :

$oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

;create new email item and display
$oOutlook = ObjCreate("Outlook.Application")
$oMessage = $oOutlook.CreateItem(0)
$oMessage.display()

;register events for the new email item
$oevent = ObjEvent($oMessage,"oMessage_")

;add an attachment
$oMessage.attachments.add("C:\windows\system32\calc.exe")

;keep script alive to receive events
while 1
    sleep(100)
WEnd

;event for when attachment is added
Func oMessage_AttachmentAdd($oAttachment)
    ConsoleWrite($oAttachment.filename & ' was added.' & @CR)
    ConsoleWrite('type=' & $oAttachment.type & @CR)
    ConsoleWrite('temp path=' & $oAttachment.GetTemporaryFilePath & @CR)
EndFunc



Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc

@Danp2 :the OP's script seems to be good compared to remarks on MSDN

Edited by jguinch
Link to comment
Share on other sites

Thanks for the better error handler.

It looks like the error is:

test.au3 (16) : ==> COM Error intercepted !
    err.number is:      0x80020009
    err.windescription: Exception occurred.

    err.description is:     Cannot access this attachment in the temporary files folder.  Use this method in an attachment event to obtain the temporary file path.
    err.source is:      Microsoft Outlook
    err.helpfile is:    
    err.helpcontext is:     0
    err.lastdllerror is:    0
    err.scriptline is:  16
    err.retcode is:     0x00001000

The MSDN page says that you will get an error if the .type is not 1 (olByValue), but in my script it is 1. It also says that you will get an error if you don't use .GetTemporaryFilePath() inside of an event callback, but I have done this.

I tried to use .GetTemporaryFilePath() outside of an event function (i just tried $oMessage.attachments(1).GetTemporaryFilePath() ) and I got the exact same com error so it's almost like it doesn't think that it's getting called from inside of an event callback for some reason...

Edited by garbb
Link to comment
Share on other sites

Can you please describe what you try to achieve? Maybe there is a simpler way to do what you want.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I have a script that creates a microsoft word document, opens it up in word to allow the user to edit it, and then after the user finishes editing it and closes it, the script creates an email, fills in the To,Cc,Subject,Body fields and adds the word document as an attachment. Then the word document file is deleted (because the attachment inside the email is a copy, so the original file is no longer needed).

Currently the way I am doing this is that my script will open the word document and wait until the user closes it, at which point it will create the email, attach the document, and then delete the document file. The problem with this is that sometimes the user will close the script and break the rest of the process (creating the email, etc...) so I was trying to do it a different/better way.

As it turns out, when you add an attachment to a new outlook email, the attachment is stored as a file in %userprofile%AppDataLocalMicrosoftWindowsTemporary Internet FilesContent.Outlook<RANDOM_STRING> and if you edit this file then you will be editing the actual email attachment. So what I was trying to do was have the script create the email and attach the file first, and then open the attachment file and allow the user to edit it. At this point the script could exit and everything that the user needs would already be open and ready to edit. The .GetTemporaryFilePath() method returns the path to the attachment file.

TLDR: After creating an email and attaching a file to it, I want to open the file.

Edited by garbb
Link to comment
Share on other sites

Wouldn't it be easier to directly send the doucment by automating Word?

Check the SendMail method.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...