Hello everyone,
I'm attempting to search through Outlook to find an attachment that is given a unique name with a date. However, there is part of the filename that is the same all of the time. I'm receiving the error on the line where I'm setting the sPath. I've got other code, that should be searching other folders for this attachment. Here is the code:
If anyone has any suggestions or ideas, I would greatly appreciate it. Thank you. Cheers!
D
I'm attempting to search through Outlook to find an attachment that is given a unique name with a date. However, there is part of the filename that is the same all of the time. I'm receiving the error on the line where I'm setting the sPath. I've got other code, that should be searching other folders for this attachment. Here is the code:
Rich (BB code):
Sub SaveAttachmentToServer()
Dim olApp As Outlook.Application, olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder, eFolder As Outlook.Folder, SpfcFolder As Outlook.Folder
Dim Atmt As Outlook.Attachment, Item As Object
Dim FileName As String, sPath As String, sFileNm As String
sPath = [CHARTER_REPLEN] & Application.PathSeparator
sFileNm = [Inbox]
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
On Error GoTo CheckInbox
Set SpfcFolder = olFolder.Folders("Davon's Emails")
For Each Item In SpfcFolder.Items
For Each Atmt In Item.Attachments
If Atmt.FileName = sFileNm Then
FileName = sPath & sFileNm
Debug.Print FileName
Atmt.SaveAsFile FileName
GoTo CleanUp
End If
Next Atmt
Next Item
CheckInbox:
On Error GoTo 0
For Each Item In olFolder.Items
For Each Atmt In Item.Attachments
If Atmt.FileName = sFileNm Then
FileName = sPath & sFileNm
Debug.Print FileName
Atmt.SaveAsFile FileName
GoTo CleanUp
End If
Next Atmt
Next Item
For Each eFolder In olFolder.Folders
For Each Item In eFolder.Items
For Each Atmt In Item.Attachments
If Atmt.FileName = sFileNm Then
FileName = sPath & sFileNm
Debug.Print FileName
Atmt.SaveAsFile FileName
GoTo CleanUp
End If
Next Atmt
Next Item
Next eFolder
MsgBox "An attachment with a file name '" & [CHARTER_REPLEN] & "' was not found", , "File Attachment."
CleanUp:
Set olApp = Nothing
Set olNs = Nothing
Set olFolder = Nothing
Set SpfcFolder = Nothing
End Sub
If anyone has any suggestions or ideas, I would greatly appreciate it. Thank you. Cheers!
D