1.

Solve : VBScript to download Exchange attachments?

Answer»

I am working on some code to download one users email and drop them into a folder. I would like to run on the exchange server so I have been using EXOLEDB. Here is what I have thus far:

For Connection
Code: [Select]Set WshShell = CreateObject("Wscript.Shell")
inboxURL = "http://server/Exchange/vzorders/inbox/testinbox" 'Mailbox Location
parsedURL = "http://server/Exchange/vzorders/Inbox/testmove/" 'Archive Location
dlFile = "\\server2\c$\testsave" 'Save File
strUserName = "user" 'Admin Username
strPassword = "password" 'Admin Password

Dim oConn
Set oConn = WScript.CreateObject("ADODB.Connection")

oConn.Provider = "ExOLEDB.DataSource"
oConn.Open = inboxURL
To try to download the attachment
Code: [Select]for each Item in colItems
' read each item in the folder
WScript.Echo "Reading " & Item.Subject
' Not needed, but good to see what is going on.
WScript.Echo " Attachments: " & Item.Attachments.Count
Set Attachments = Item.Attachments
if Attachments.Count > 0 then
' Add a statement about removing the file here
Item.HTMLBody = Item.HTMLBody & vbVr & _
"<p> <p>" ' TODO: Add a statement about moving the files
Item.Save
end if

Dim AttachmentsCount
AttachmentsCount=Attachments.Count
for index = AttachmentsCount To 1 Step -1
Set Attachment = Attachments(index)
WScript.Echo " Saving " & Attachment.FileName
' Again not needed, just to see what is going on.
Dim SaveName
'Dim TmpName 'Uncomment all Tmpname reference to add extension in front of name
Dim Name
'TmpName = FSO.GetTempName
' This helps avoid clobering files with the same name
' but really there should be a filesystemobject check to be 100% sure.
Name = Attachment.FileName 'TmpName & "__" & Attachment.FileName
SaveName = SaveFolder & Name
Attachment.SaveAsFile SaveName

' Uncomment the following if you would like to download and create a link to the attachment
' Attachment.Delete
' Dim Body
' Body = Item.HTMLBody & vbVr & "<a href='"&SaveName&"'>" & SaveName & "</a><br>"
' Item.HTMLBody=Body
' Item.Save

' If you want to simply delete the attachments you can axe the SaveAsFile
' and all the changes made to the Body.HTMLBody
On Error Resume Next
next


On Error Resume Next
nextThe two sections are compiled snippets I found and I might be going about this completely wrong, any advice would be great.Not familiar with the ExOLEDB provider, but this looks like something I did to grab attachments from Outlook emails.

There is nothing destructive in the code. Have you tried running it? Any error MESSAGES? It's easier to debug something once we have a clue what it is doing versus what you want it to do.

To start on line 14, (for each Item in colItems) I get "Object is not a COLLECTION".I should have NOTICED this the first time. The script shows a ADO connection. Seems reasonable you'll need an ADO recordset and a SQL Select statement.

Code: [Select] strSql = "";
strSql = "select ";
strSql = strSql & " \"urn:schemas:mailheader:content-class\"";
strSql = strSql & ", \"DAV:href\" ";
strSql = strSql & ", \"DAV:displayname\"";
strSql = strSql & " from scope ('shallow traversal of " + "\"";
strSql = strSql & sFdUrl + "\"') ";
strSql = strSql & " WHERE \"DAV:ishidden\" = false";
strSql = strSql & " AND \"DAV:isfolder\" = false";

As mentioned, I'm not familiar with Exchange or it's notation.

Microsoft Eample

Note: the example is C++; the SQL is the same (replace + with &)

Good luck. I noticed in the example they defined a recordset and some fields as well, is that something I need to try?I would think so. Once the data is in the recordset, you'll need to make changes to any FIELD names in the download attachment section:

Code: [Select]Set WshShell = CreateObject("Wscript.Shell")
inboxURL = "http://server/Exchange/vzorders/inbox/testinbox" 'Mailbox Location
parsedURL = "http://server/Exchange/vzorders/Inbox/testmove/" 'Archive Location
dlFile = "\\server2\c$\testsave" 'Save File
strUserName = "user" 'Admin Username
strPassword = "password" 'Admin Password

Dim oConn
Set oConn = WScript.CreateObject("ADODB.Connection")
set oRS = WScript.CreateObject("ADODB.Recordset") 'added record set

oConn.Provider = "ExOLEDB.DataSource"
oConn.Open = inboxURL

strSql = "";
strSql = "select ";
strSql = strSql & " \"urn:schemas:mailheader:content-class\"";
strSql = strSql & ", \"DAV:href\" ";
strSql = strSql & ", \"DAV:displayname\"";
strSql = strSql & " from scope ('shallow traversal of " + "\"";
strSql = strSql & sFdUrl + "\"') ";
strSql = strSql & " WHERE \"DAV:ishidden\" = false";
strSql = strSql & " AND \"DAV:isfolder\" = false";
oRS.Open strSQL, adoCon, 2

oRS.MoveFirst
do until not oRS.EOF
wscript.echo oRS.item(0), oRS.item(1) oRS.item(2)
oRS.MoveNext
loop

You'll need to fixup the SQL statements. When done use cscript to test. I added
that last loop to print the recordset. Might be wise to see what we're dealing with before proceeding.



Discussion

No Comment Found