Home > Mobile >  Saving specific attachment in Outlook VBA Error
Saving specific attachment in Outlook VBA Error

Time:10-03

I've setup an outlook rule to run the following script when a specific email comes in. I want the code to look in the specific folder, see if there is currently a file called CID.csv and if so delete it before saving the new CID.csv file into it.

Everything works except my line for saving the attachment. The code error I get is Object variable or With block variable not set.

I'm not sure what I'm suppose to set objAtt to exactly to fix this so my save goes off.

Any help would be appreciated.

Public Sub saveAttachtoDisk(item As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim strFileName As String
Dim STRFileExists As String
Dim strName As String
Dim EmAttFile As String
Dim i As Long
Dim EmAttachCount As Long

On Error GoTo ErrHandler

saveFolder = "\\page\data\NFInventory\groups\CID\Schedules\Nightly File Schedules\Deb's File Autosave\Extra Packs"
strFileName = "\\page\data\NFInventory\groups\CID\Schedules\Nightly File Schedules\Deb's File Autosave\Extra Packs\CID.csv"
STRFileExists = Dir(strFileName)

Set EmAttach = item.Attachments
    AttachCount = EmAttach.Count
    
For i = AttachCount To 1 Step -1

' Get the file name
    EmAttFile = EmAttach.item(i).FileName
        If EmAttFile = "CID.csv" Then
        
           If STRFileExists = "" Then
               'MsgBox "The selected file doesn't exist"
               objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
           Else
               MsgBox "The selected file exists"
               Kill "S:\NFInventory\groups\CID\Schedules\Nightly File Schedules\Deb's File Autosave\Extra Packs\CID.csv"
               objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
           End If

        End If
Next i

ErrHandler:
If Err.Number <> 0 Then
MsgBox "Error Number:" & Err.Number & vbCrLf & _
    "Error Description: " & Err.Description
Exit Sub
Else
End If
End Sub

CodePudding user response:

While not an answer per se, you may find a generic save attachment function useful:

With it you can use or set a number of different options (such as only saving the ".pdf,.txt" files, or deleting any saved attachments or overwriting existing files)

NB: you should actually be able to use FileExtensions:="\CID.csv" to save a specific file

Private Function SaveAttachments(ByVal Item As Object, FilePath As String, _
    Optional Prefix As String = "", _
    Optional FileExtensions As String = "*", _
    Optional Delimiter As String = ",", _
    Optional RemoveAttachments As Boolean = False, _
    Optional OverwriteFiles As Boolean = False) As Boolean

    On Error GoTo ExitFunction

    Dim i As Long, j As Long, FileName As String, Flag As Boolean
    Dim Extensions() As String: Extensions = Split(FileExtensions, Delimiter)
    If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"

    For j = LBound(Extensions) To UBound(Extensions)
        With Item.Attachments
            If .Count > 0 Then
                For i = .Count To 1 Step -1
                    FileName = FilePath & Prefix & .Item(i).FileName
                    Flag = IIf(LCase(Right(FileName, Len(Extensions(j)))) = LCase(Extensions(j)), True, False)
                    Flag = IIf(FileExtensions = "*" Or Flag = True, True, False)
                    If Flag = True Then
                        If Dir(FileName) = "" Or OverwriteFiles = True Then
                            .Item(i).SaveAsFile FileName
                        Else
                            Debug.Print FileName & " already exists"
                            Flag = False
                        End If
                    End If
                    If RemoveAttachments = True And Dir(FileName) <> "" And Flag = True Then .Item(i).Delete
                Next i
            End If
        End With
    Next j
    SaveAttachments = True

ExitFunction:
End Function

CodePudding user response:

You should define objAtt before to use it:

...
For i = AttachCount To 1 Step -1
    EmAttFile = EmAttach.item(i).FileName
    If EmAttFile = "CID.csv" Then   
 
       objAtt = EmAttach.item(i)       ' <---- Define objAtt before to use it

       If STRFileExists = "" Then
           'MsgBox "The selected file doesn't exist"
           objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
       Else
           MsgBox "The selected file exists"
           Kill "S:\NFInventory\groups\CID\Schedules\Nightly File Schedules\Deb's File Autosave\Extra Packs\CID.csv"
           objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
       End If

    End If
Next i
...
  • Related