bourny Posted July 30, 2016 Posted July 30, 2016 I have a working script that sends an email without issue, I wish to however instead of sending it , save it to a .msg or .oft for somebody else to pick it up off a share and send it. I have noticed the correct code to use is ".SaveAs" object but I cannot seem to get any variations to work. example of this is https://msdn.microsoft.com/en-us/library/office/ff868727.aspx My code is expandcollapse popup#include <File.au3> Global $oMyRet[2] Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; OlSaveAsType Constants Global Const $olTXT = 0 Global Const $olRTF = 1 Global Const $olTemplate = 2 Global Const $olMSG = 3 Global Const $olDoc = 4 $SetupSMTP_Host = "mailserver.co.uk" $EmailName = "script" $FromAddress = "me@me.co.uk" $ToEmail = "you@you.co.uk" $EmailSubject = "test" $Body = "Test Report" $AttachFiles = "" $EmailCC = "" $EmailBCC = "" $SetupSMTP_Importance = "Normal" $SetupSMTP_Username = "ussr" ;UserID $SetupSMTP_Password = "pass" ;PassWD $SetupSMTP_Port = 25 $SetupSMTP_Sll = 1 $ReplyTo = "" $rc = _INetSmtpMailCom($SetupSMTP_Host, $EmailName, $FromAddress, $ToEmail, $EmailSubject, $Body, $AttachFiles, $EmailCC, $EmailBCC, $SetupSMTP_Importance, $SetupSMTP_Username, $SetupSMTP_Password, $SetupSMTP_Port, $SetupSMTP_Sll, $ReplyTo) Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $SetupSMTP_Port = 25, $SetupSMTP_Sll = 0, $s_ReplyTo = "") Local $objEmail = ObjCreate("CDO.Message") ;$objEmail.Subject = "Status Report" ;$objEmail.To = "status@you.co.uk" ;$objEmail.Display ;$objEmail.SaveAs ("HOMEPATH" & "\My Documents\statusrep.oft", OlSaveAsType.olTemplate) $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>' $objEmail.To = $s_ToAddress $objEmail.ReplyTo = $s_ReplyTo Local $i_Error = 0 Local $i_Error_desciption = "" If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress $objEmail.Subject = $s_Subject If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then $objEmail.HTMLBody = $as_Body Else $objEmail.Textbody = $as_Body & @CRLF EndIf ;Split up multiple file attachments by ; If $s_AttachFiles <> "" Then Local $S_Files2Attach = StringSplit($s_AttachFiles, ";") For $x = 1 To $S_Files2Attach[0] $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x]) ;ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console If FileExists($S_Files2Attach[$x]) Then $objEmail.AddAttachment ($S_Files2Attach[$x]) Else ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF) SetError(1) Return 0 EndIf Next EndIf $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer If Number($SetupSMTP_Port) = 0 then $SetupSMTP_Port = 25 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $SetupSMTP_Port If $s_Username <> "" Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password EndIf If $SetupSMTP_Sll Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True EndIf $objEmail.Configuration.Fields.Update Switch $s_Importance Case "High" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High" Case "Normal" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal" Case "Low" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low" EndSwitch $objEmail.Fields.Update $objEmail.Saveas( "c:\temp\test.oft", OlSaveAsType.olTemplate ) ;;;;;;THIS BIT DOES NOT WORK If @error Then SetError(2) Return $oMyRet[1] EndIf $objEmail="" EndFunc Func MyErrFunc() $HexNumber = Hex($oMyError.number, 8) $oMyRet[0] = $HexNumber $oMyRet[1] = StringStripWS($oMyError.description, 3) ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF) SetError(1) Return EndFunc
water Posted July 31, 2016 Posted July 31, 2016 You are mixing two things: You want Outlook functionality but the script you posted is using CDO. If you need Outlook then my OutlookEX UDF with function _OL_ItemSave should do the job. 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
bourny Posted July 31, 2016 Author Posted July 31, 2016 I am trying to use the above script as its the only way I can seem to formulate and email and then successfully send it. this is why I was going down the route of using the $objemail.saveas script. I have tried the outlookex but struggling to get it to send and email. this is the code I am testing at the moment. i have had to rem out the folder access bit as it keeps failing with the folder does not exist message. If I rem it out it goes a bit further then fails on the item send bit. #include <OutlookEx.au3> $iOL_Debug = 2 ; Open the connection to Outlook Global $oOL = _OL_Open() ; Access the default mail folder ;Global $aFolder = _OL_FolderAccess($oOL, "", "", $olMailItem) ;MsgBox(0, "", "FolderAccess: @error = " & @error & ", @extended = " & @extended) ; Create a mail item and set some properties Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "", "", _ "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in <b>Test</b>.", "SentOnBehalfOfName=Doe Jane") MsgBox(0, "", "ItemCreate: @error = " & @error & ", @extended = " & @extended) ; Add a recipient and resolve it _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, "mymailbox@mymail.co.uk") MsgBox(0, "", "Add Recipient: @error = " & @error & ", @extended = " & @extended) ;Send the mail _OL_ItemSend($oOL, $oItem) If @extended = -2147352567 Then _ MsgBox(16, "OutlookEX UDF - Example Script", "You don't have permission to send the mail on behalf of the specified person!") This is why I have stayed with the CDO script I have above as its 100% working but I just need to figure out the CDO code bit to save the email. I am sure i am on the right tracks with this line and just need to make a slight adjustment to complete it. $objEmail.Saveas( "c:\temp\test.oft", OlSaveAsType.olTemplate ) ;;;;;;THIS BIT DOES NOT WORK
water Posted July 31, 2016 Posted July 31, 2016 "Does not work" is a bit vague. Do you get any error message? What is the value of @error or @extended after you called the function? Do you have implemented a COM error handler? 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
bourny Posted July 31, 2016 Author Posted July 31, 2016 point well made. When I execute without anything remmed out I get folderaccess msgbox with @error = 1 which means folder type is missing or not a number. i then get a failure with this in the Scite debug "C:\Program Files (x86)\AutoIt3\Include\OutlookEx.au3" (4119) : ==> The requested action with this object has failed.: $vItem.Send() $vItem^ ERROR
water Posted July 31, 2016 Posted July 31, 2016 Which version of AutoIt do you run? 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
water Posted July 31, 2016 Posted July 31, 2016 Do you run the lastest version of the OutlookEX UDF? Version 1.2.1.0 (released 20th July) fixed a bug with AutoIt versions > 3.3.12.0 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
bourny Posted July 31, 2016 Author Posted July 31, 2016 little further now I just get I have not got permissions to send on behalf of. Clearly i am using this UDF all wrong.
water Posted July 31, 2016 Posted July 31, 2016 "SentOnBehalfOfName=Doe Jane" is only needed if you want to send the mail in the name of another user. If you do not want to then simply delete this parameter. And I doubt you have a "Jane Doe" in your company 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
bourny Posted July 31, 2016 Author Posted July 31, 2016 getting somewhere now. I have managed to create a mail item and save it as a .msg file. This pretty much is what I need to achieve. I will test some more functions but it seems I had an incorrect version of the UDF. Here is the code I am using I have had to strip down the _OL_iemCreate as the example had a folder in the mailbox but I cannot seem to define a folder to create them item in so removing it and leaving a blank field seems to work., not sure where it is dumping the mail item in the mailbox but at least it saves it to disk. ; ***************************************************************************** ; Example 4 ; Create a html mail plus two attachments (plus one inline picture = attachment) ; but don't send it ; ***************************************************************************** ; Create the item without setting the body. We first need to add the picture before we can refer to in by the HTML body. $oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", "", "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in <b>bold</b>.", "To=myemailaddress") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail in folder 'Outlook-UDF-Test\TargetFolder\Mail'. @error = " & @error & ", @extended = " & @extended) _OL_ItemSave($oOutlook, $oItem, Default, "C:\temp\Outlook-UDF-Test\Dir1\", $olMSGUnicode) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemSave Example Script", "Error saving mail item to C:\temp\Outlook-UDF-Test\Dir1\. @error = " & @error & ", @extended = " & @extended) ;ShellExecute("C:\temp\Outlook-UDF-Test\Dir1\TestMail.html") MsgBox(64, "OutlookEX UDF: _OL_ItemSave Example Script", "Example 1: Item successfully saved!")
bourny Posted July 31, 2016 Author Posted July 31, 2016 to be more specific it was $vFolder that I had to remove. not sure what this does.
water Posted July 31, 2016 Posted July 31, 2016 Where do you use $vFolder? Can not find it in the code you posted. 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
bourny Posted July 31, 2016 Author Posted July 31, 2016 I am referring to your code in your outlookex udf. If I pass in a 3rd parameter I think it refers to a folder in my outlook inbox that does not exist. If I blank it out the script works. is there a list of fields I can reference such as bcc, bc, etc. ; #FUNCTION# ==================================================================================================================== ; Name...........: _OL_ItemCreate ; Description ...: Creates an item. ; Syntax.........: _OL_ItemCreate($oOL, $iItemType[, $vFolder = ""[, $sTemplate = ""[,$sP1 = ""[, $sP2 = ""[, $sP3 = ""[, $sP4 = ""[, $sP5 = ""[, $sP6 = ""[, $sP7 = ""[, $sP8 = ""[, $sP9 = ""[, $sP10 = ""]]]]]]]]]]]]) ; Parameters ....: $oOL - Outlook object returned by a preceding call to _OL_Open() ; $iItemType - Type of item to create. Is defined by the Outlook OlItemType enumeration ; $vFolder - Optional: Folder object as returned by _OL_FolderAccess or full name of folder where the item will be created.
water Posted August 1, 2016 Posted August 1, 2016 What is the value of @extended after calling _OL_ItemCreate? 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now