Sign in to follow this  
Followers 0
Naveed

Outlook SaveAs Method

8 posts in this topic

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



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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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
EndFunc

This 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)
Next

ERROR : 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

#4 ·  Posted (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
EndIf

To select a mail by subject , sender etc. you can use the _OutlookSaveMail function of the

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

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

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..... :x

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

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
Sign in to follow this  
Followers 0