Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

JScript to search for specific outlook emails, and download attachments

$
0
0
Hi,

I just ported this from our VBScript version to Jscript. More for fun, than anything else.


Code:

' /*
'        Search for and Download daily email attachment
'      Get_Attachment_from_Email.wsf
' */

<package>
        <job id="Get_Invoice_Attachment_from_Email">
                <script language="VBScript" src="UDFS.vbs" >
                </script>
                <script language="JScript">
       

                        "use strict"
                       
                        var olFolderInbox = 6

                        var ReturnValue_x

                        var Outlook_o
                        var Namespace_o
                        var DefaultFolder_o
                        var Items_col
                        var Filtered_Items_col
                        var Message_o
                        var Message_Count_n
                        var NthMessage_n

                        var Attachment_o
                        var Attachment_Count_n
                        var Attachment_Item_o

                        var version_s

                        var Subject_s
                        var Body_s
                        var ThisFolder
                       
                        var downloade_folder_s
                        var FullFilePath_s

                        Subject_s = "Daily Invoices"
                        downloade_folder_s = "c:\\FolderToSaveAttachments\\Invoices"
                       
                        try
                                {

                                        version_s = WScript.Version        // Probably 5.812
                                        WScript.Echo( "ECMAScript version:" + version_s ) 

                                        Outlook_o = WScript.CreateObject("Outlook.Application")
                                        Namespace_o = Outlook_o.GetNamespace("MAPI")
                                        DefaultFolder_o = Namespace_o.GetDefaultFolder(olFolderInbox) // Inbox
                                        Items_col = DefaultFolder_o.Items
                                        Filtered_Items_col = Items_col.Restrict("[Subject] = " + Subject_s)
                                        // We want only emails and attachments from today
                                        Filtered_Items_col = Filtered_Items_col.Restrict("@SQL=%today(" + String.fromCharCode(34) + "urn:schemas:httpmail:datereceived" + String.fromCharCode(34) + ")%")

                                        Message_Count_n = Filtered_Items_col.Count
                                        WScript.Echo( "Message Count " + Message_Count_n )

                                        for ( NthMessage_n = Message_Count_n ; NthMessage_n > 0 ; NthMessage_n-- )
                                        {
                                                Message_o = Filtered_Items_col.Item( NthMessage_n )
                                                Subject_s = Message_o.Subject
                                                // WScript.Echo( Subject_s )
                                                Attachment_Count_n = Message_o.Attachments.Count
                                                if (Attachment_Count_n>0)
                                                {
                                                        FileName_s = Message_o.Attachments.Item(1).FileName
                                                        FullFilePath_s = downloade_folder_s + "\\" + FileName_s
                                                        WScript.Echo( FullFilePath_s )
                                                        Message_o.Attachments.Item(1).SaveAsFile( FullFilePath_s )
                                                        break
                                               
                                                }
                                        }
                                }
                                catch(exception)
                                {
                                        WScript.Echo( "What the...?" )
                                        WScript.Echo( exception )
                                        WScript.Quit(-1)
                                }
                                finally
                                {
                                        WScript.Echo( "Finally." )
                                        WScript.Quit(0)
                                }
                       
                </script>
        </job>
</package>


Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>