Jump to content

Outlook SaveAs Method


Naveed
 Share

Recommended Posts

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

Link to comment
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 2022-02-19 - Version 1.6.1.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 (NEW 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

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

???

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.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 (NEW 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

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

???

Link to comment
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

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