saving attachments in local map

M

Martijn

got a major issue;

I receive xml files by mail and want to save them automaticly in a map.
(my file is called: client.xml)

I found that i had to use VB in outlook and puted in:

thanks to:
http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_21135312.html

***
Public Sub saveAttachtoDisk (itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment

Dim saveFolder As String
saveFolder = "c:\temp\"

For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
*** end code

It is working fine, but the file is overwriting the file that is saved
already.

How can I change the code that if there is a file client.xml the new file is
saved as client (1).xml etc so that the original file doesn't get lost.

Thanx
 
D

David Lloyd

Martijn:

One alternative is use the FileSystemObject to check for the existence of
the file. You will need a reference to the Microsoft Scripting Runtime to
use the FileSystemObject. The following sample shows one way to accomplish
this task. This code assumes that the DisplayName property holds a valid
filename with extension.

Public Sub saveAttachtoDisk (itm As Outlook.MailItem)
Dim fso As New Scripting.FileSystemObject
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim sFileBase As String
Dim sFile As String
Dim sExtension As String
Dim i As Integer
Dim iPeriod As Integer

saveFolder = "c:\temp\"
i = 1
For Each objAtt In itm.Attachments
iPeriod = InStrRev(objAtt.DisplayName, ".")
sFileBase = Left(objAtt.DisplayName, iPeriod - 1)
sFile = sFileBase
sExtension = Mid(objAtt.DisplayName, iPeriod)

Do Until Not fso.FileExists(saveFolder & sFile & sExtension)
sFile = sFileBase & CStr(i)
i = i + 1
Loop
objAtt.SaveAsFile saveFolder & sFile & sExtension
Set objAtt = Nothing
i = 1
Next

Set fso = Nothing
Set objAtt = Nothing
End Sub

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


got a major issue;

I receive xml files by mail and want to save them automaticly in a map.
(my file is called: client.xml)

I found that i had to use VB in outlook and puted in:

thanks to:
http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_21135312.html

***
Public Sub saveAttachtoDisk (itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment

Dim saveFolder As String
saveFolder = "c:\temp\"

For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
*** end code

It is working fine, but the file is overwriting the file that is saved
already.

How can I change the code that if there is a file client.xml the new file is
saved as client (1).xml etc so that the original file doesn't get lost.

Thanx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top