Naveed 0 Posted December 20, 2010 Guys, I hope somebody can shed light on this, basically I am trying the below method to save the currently selected item in Outlook 2007 as a .txt or .msg file. I have tried to convert from vbs to au3 but i get a random result and i cant make sense of it and it doesn't work. Here is the code; Sub SaveAsTXT() Dim myItem As Outlook.Inspector Dim objItem As Object Set myItem = Application.ActiveInspector If Not TypeName(myItem) = "Nothing" Then Set objItem = myItem.CurrentItem strname = objItem.Subject 'Prompt the user for confirmation Dim strPrompt As String strPrompt = "Are you sure you want to save the item? " & _ "If a file with the same name already exists, " & _ "it will be overwritten with this copy of the file." If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then objItem.SaveAs Environ("HOMEPATH") & "\My Documents\" & strname & ".txt", olTXT End If Else MsgBox "There is no current active inspector." End If End Sub Any help will be much appreciated. Thanks Share this post Link to post Share on other sites
water 2,392 Posted December 20, 2010 Hi naveed, can you show your AutoIt code you have so far? Maybe it's jsut a translation problem. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
Naveed 0 Posted December 20, 2010 This was the code generated by the converter.; ---------------------------------------------------------------------------- ; ; VBScript to AutoIt Converter v0.4 ; ; ---------------------------------------------------------------------------- Func SaveAsTXT() Dim $myItem $As $Outlook.Inspector[] Dim $objItem $As $Object $myItem = $Application.ActiveInspector If Not TypeName($myItem) = "Nothing" Then $objItem = $myItem.CurrentItem $strname = $objItem.Subject ;Prompt the user for confirmation Dim $strPrompt $As String $strPrompt = "Are you sure you want to save the item? " & _ "If a file with the same name already exists, " & _ "it will be overwritten with this copy of the file." If MsgBox($strPrompt, $vbYesNo + $vbQuestion) = $vbYes Then $objItem.SaveAs ($Environ("HOMEPATH") & "\My Documents\" & $strname & ".txt", $olTXT) EndIf Else MsgBox "There is no current active inspector." EndIf EndFuncThis is the code I have extracted from the Outlook Object Model and put together myself, but i get an error when running this...#include <Outlook.au3> $Outlook = ObjCreate("Outlook.Application") $oNameSpace = $Outlook.GetNameSpace("MAPI") $olExp = $Outlook.ActiveExplorer() $olSel = $olExp.GetSelection() For $i = 1 to $i <= $olsel.GetCount() Step +1 $olSelectedItem = $olSel.Item() ; Get the selected item $strname = $olSelectedItem.Subject $sSaveDir = @ScriptDir $sSubject = $strname $sSuffix = ".msg" $sSaveFormat = $olMSG $olSelectedItem.SaveAs ($sSaveDir & $sSubject & $sSuffix, $sSaveFormat) NextERROR : The requested action with this object has failed.:$olSel = $olExp.GetSelection()$olSel = $olExp.GetSelection()^ ERROR->14:32:40 AutoIT3.exe ended.rc:1??? Share this post Link to post Share on other sites
water 2,392 Posted December 20, 2010 (edited) This is the translation of the VB code to save the currently opened mail item:$oOutlook = ObjCreate("Outlook.Application") $sWindowTitle = "Outlook Save As" $oMyItem = $oOutlook.ActiveInspector If IsObj($oMyItem) Then $oItem = $oMyItem.CurrentItem $sSubject = $oItem.Subject $sPrompt = "Are you sure you want to save the item? " & _ "If a file with the same name already exists, " & _ "it will be overwritten with this copy of the file." If MsgBox(36, $sWindowTitle, $sPrompt) = 6 Then $oItem.SaveAs(@MyDocumentsDir & "\" & $sSubject & ".txt", 0) ConsoleWrite(@error & @CRLF) Else MsgBox(16, $sWindowTitle, "There is no current active inspector.") EndIf EndIfTo select a mail by subject , sender etc. you can use the _OutlookSaveMail function of the Edited December 20, 2010 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
Naveed 0 Posted December 20, 2010 Hi Water, Thanks for your reply, i have tried your suggestion but i still get an error message. I have stripped out the other stuff and here is the code; #INCLUDE <Outlook.au3> $oOutlook = ObjCreate("Outlook.Application") $sWindowTitle = "Outlook Save As" $oMyItem = $oOutlook.ActiveInspector $oItem = $oMyItem.CurrentItem $sSubject = $oItem.Subject MsgBox(1,"",$sSubject) $oItem.SaveAs(@ScriptDir & "\" & $sSubject & ".msg", $OlMSG) I get this error: The requested action with this object has failed.: $oItem = $oMyItem.CurrentItem $oItem = $oMyItem.CurrentItem^ ERROR ??? Share this post Link to post Share on other sites
Zedna 280 Posted December 20, 2010 Add COM error handler to see detailed errors.See the ObjEvent in the helpfile Resources UDF ResourcesEx UDF AutoIt Forum Search Share this post Link to post Share on other sites
Juvigy 49 Posted December 20, 2010 This is because you havent open the mail. First you need to double click one mail and open it to another window. Then run the code. Share this post Link to post Share on other sites
Naveed 0 Posted December 20, 2010 Thanks for your help guys, I figured it out in the end, I needed to find the object selected in the Main Outlook explorer window. Rather than a single open Email/MailItem window. It only took me the whole afternoon to work it out..... Here is how i did it; #include <Outlook.au3> $oOutlook = ObjCreate("Outlook.Application") For $objItem In $oOutlook.ActiveExplorer.Selection $sSubject = $objItem.Subject MsgBox(1,"",$sSubject) $objItem.SaveAs("c:\temp" & "\" & StringRegExpReplace($sSubject,'[\/:*?"<>|]', '_') & ".msg", $olMSG) Next Enjoy...... Naveed Share this post Link to post Share on other sites