We help IT Professionals succeed at work.

have a batch file check an outlook folder and open attachments

Dier02
Dier02 asked
on
513 Views
Last Modified: 2012-05-05
I want to use a batch file to do the following:

1. Open a specific folder in Outlook.
2. Open new e-mails and their attachments (which are xml files).
3. Save the xml attachments to another folder on the desktop.
4. Import the XML files into an Access database.
Comment
Watch Question

Try this VBscript for extracting the attachments.  Don't know how to import to Access I'm afraid.

'--- Define the folder name
strFolderName="MyFolder"
'--- Define the path to save attachments to
strSavePath="C:\Temp\"

'--- Define constant for Inbox
Const olFolderInbox = 6

'--- Create objects for outlook application, namespace and defaul inbox
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)

'--- Connect to folder
Set objFolder = objInbox.Folders(strFolderName)

'--- Create object of items in folder
Set objItems = objFolder.Items

'--- For each e-mail, check the attachment count, and if above 0, save
For each objMail in objItems
      msgbox objMail.Subject
      if objMail.Attachments.Count > 0 then
            For i = 1 To objMail.Attachments.Count
                  objMail.Attachments.Item(i).SaveAsFile strSavePath &  objMail.Attachments.Item(i).FileName
              Next
      end if
Next

'--- Delete objects
Set objItems = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
Set objApp = Nothing
BTW, I forgot to remove the following line I inserted while testing:
      msgbox objMail.Subject

Author

Commented:
Where do I write the script in and what/how do I use it?  What program do I need to write the import script in - in Outlook?  In Excel? and once I add the VB how do I initiate it?

Author

Commented:
Ok, so I write it in notepad:) and save iit as a vbs file.  Now,  I have named my inbox folder xml_attachments and my documents reside on d drive.  I have a folder in My Documents called "xml_".  How do I add this to the above to have the attachments stripped from the mail in the "xml_attachments" folder in Outlook and placed in the "xml_" folder in My Docs?
Find the actual path to 'my documents'
e.g. C:\Documents and settings\<username>\my documents\xml_

Change the first bit:

'--- Define the folder name
strFolderName="xml_attachments"            <=== add the Outlook folder here.
'--- Define the path to save attachments to
strSavePath="<path-to-my-documents>"   <== add the path to 'My Documents' and your folder in it here

Author

Commented:
I get an error coming up
path:C:\Documents and Settings\dlon0\My Documents\xml_
Script ......
Line 1
Char 1
Error: Expected statement
Code 800A400
Source: Microsoft VB script compilation error
The first 4 lines should be as below.  What have you got?

'--- Define the folder name
strFolderName="xml_attachments"
'--- Define the path to save attachments to
strSavePath="C:\Documents and Settings\dlon0\My Documents\xml_\"

Author

Commented:
same as that
the error message states the error is on line 1, which is a comment as the first character is a single quote '

Re-enter a single quote at the beginning of the line, in case you have a different but similar character present.

My example included comments which all start with '---
You could just remove the comment line as it's not required.

Author

Commented:
Error:
Line 23 (after comments have been removed)
Char 6
Syntax error
Code 800A03EA
Source MS VB script compilation error

Author

Commented:
line 23 reads "back to top" so removed that.
Outlook is on Exchange on server.(don't know if relevant)

Code looks like following:
strFolderName="xml_attachments"
strSavePath="d:\danoo0\xml_"
Const olFolderInbox = 6
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objFolder = objInbox.Folders(strFolderName)
Set objItems = objFolder.Items
For each objMail in objItems

      if objMail.Attachments.Count > 0 then
            For i = 1 To objMail.Attachments.Count
                  objMail.Attachments.Item(i).SaveAsFile strSavePath &  objMail.Attachments.Item(i).FileName
              Next
      end if
Next
Set objItems = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
Set objApp = Nothing

Author

Commented:
Now code doing nothing.  No error notes and also not moving file after I opened Outlook then clicked on the script.
I had assumed that "xml_attachments" was a sub-folder of your default inbox.  If it IS your default inbox, try this.

Note1: The save path strSavePath needs the backslash at the end.
Note2: Your script had 'back to top' in it as you copied it from the web page (see bottom right corner of the comment box) - nothing to do with me!

strSavePath="d:\danoo0\xml_\"
Const olFolderInbox = 6
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objItems = objInbox.Items
For each objMail in objItems
      if objMail.Attachments.Count > 0 then
            For i = 1 To objMail.Attachments.Count
                  objMail.Attachments.Item(i).SaveAsFile strSavePath &  objMail.Attachments.Item(i).FileName
            Next
      end if
Next
Set objItems = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
Set objApp = Nothing

Author

Commented:
no it is a sub-folder.  I will try the script later but want to know if the mailbox is located on an organisations exchange server if the script could have any negative  effect on that server.  Is it non-destructive?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.