[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

have a batch file check an outlook folder and open attachments

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.
0
Dier02
Asked:
Dier02
  • 8
  • 7
1 Solution
 
charliebiscottiCommented:
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
0
 
charliebiscottiCommented:
BTW, I forgot to remove the following line I inserted while testing:
      msgbox objMail.Subject
0
 
Dier02Author 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?
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Dier02Author 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?
0
 
charliebiscottiCommented:
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
0
 
Dier02Author 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
0
 
charliebiscottiCommented:
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_\"
0
 
Dier02Author Commented:
same as that
0
 
charliebiscottiCommented:
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.

0
 
Dier02Author Commented:
Error:
Line 23 (after comments have been removed)
Char 6
Syntax error
Code 800A03EA
Source MS VB script compilation error
0
 
Dier02Author 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
0
 
Dier02Author Commented:
Now code doing nothing.  No error notes and also not moving file after I opened Outlook then clicked on the script.
0
 
charliebiscottiCommented:
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
0
 
Dier02Author 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?
0
 
charliebiscottiCommented:
The script doesn't change anything in the mailbox at all, it just saves the attachments.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now