Code help

  • Thread starter Thread starter Satalerba
  • Start date Start date
S

Satalerba

Hi.
Some months ago I posted a question about how to
manipulate attachments.
I received some answers and one of them worked just great.
It reads all the inbox messages, saves the attachment of
each one in a specific path, deletes the attachment from
the original message and saves the message (without the
attachment) in the inbox.
The only trouble I have found is the code doesn't
recognize if the attached file exists or not in the path.
For example, if the file "file.xls" exists, and there is
another message with the same "file.xls" attached, the
code overwrites and replaces the first "file.xls".
The code is executed in Excel.

--------------------------------------------------

Option Explicit
Sub macrosOutlook()
Dim aplicacion
Dim sesion
Dim correos
Dim item
Dim adjunto
Dim n
Dim nombreArchivo

Const olFolderInbox = 6
Const olMail = 43
Const sSavePath = "c:\MyDocuments\"

Set aplicacion = CreateObject("Outlook.Application")
Set sesion = aplicacion.GetNamespace("MAPI")

sesion.Logon

Set correos = sesion.GetDefaultFolder(olFolderInbox).Items

For Each item In correos
' If item.Class = olMail Then
If InStr(item.MessageClass, "IPM.Note") = 1 Then
If item.Attachments.Count <> 0 Then
For n = item.Attachments.Count To 1 Step -1
Set adjunto = item.Attachments.item(n)
nombreArchivo = sSavePath & adjunto.FileName
adjunto.SaveAsFile nombreArchivo
adjunto.Delete
Next
item.Save
End If
End If
Next
End Sub

--------------------------------------------------

How should I modify the code to be able to recognize if
the attached file exists or not, if it does, then it
should be saved as "fileA.xls", and if "fileA.xls" exists,
then as "fileB.xls", and so on.
Thanks in advance.
Satalerba.
 
One way would be to use Windows Scripting and the FileSystemObject's
FleExists method.

Dim fso As Scripting.FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(nombreArchivo) Then
'it exists already
 
Back
Top