Results 1 to 2 of 2

Thread: Saving Attachment from Outlook to a specified location on my computer

Hybrid View

  1. #1
    Member
    Join Date
    Jun 12
    Posts
    34

    Question Saving Attachment from Outlook to a specified location on my computer

    Good morning all!!!

    I am looking for some code that will search for a specific subject line and then take the attachment and save as an excel worksheet to a specified destination. I have been playing with the following code...

    Code:
    Sub SaveFile()
    
    Dim olApp As Outlook.Application
    Dim olNs As Namespace
    Dim Fldr As MAPIFolder
    Dim olMi As MailItem
    Dim olAtt As Attachment
    Dim Mypath As String
    Dim i As Long
    Dim InStr As Object
    
    
    Set olApp = Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    Set MoveToFldr = Fldr.Folders("Repair Reports")
    Mypath = "S:\Departments\Service & Production\Public\Repair Reports"
    
    
    For i = Fldr.Items.Count To 1 Step -1
        If olMi.Subject = "Daily repair report" Then
            For Each olAtt In olMi.Attachments
                If olAtt.Filename = "_repairs.csv" Then
                    Attachment.SaveAsFile Mypath & olMi.SenderName & ".xlsx"
                    End If
                Next olAtt
            olMi.Save
            olMi.Move MoveToFldr
            
        End If
    Next i
    
    Set olAtt = Nothing
    Set olMi = Nothing
    Set Fldr = Nothing
    Set MoveToFldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
    
    End Sub
    I keep getting block errors and even when I thought I resolved those errors and the macro runs, nothing happens. Here is some additional background. I get an email every Mon through Friday at 9am and the attachment is dated for the day before.

    Ex.
    I received the email this morning(8/15) and the attachment is named...8-14-2012_repair report.csv

    I would like to modify the code so it saves the file as just the date and as an excel file not a CSV file.

    I look forward to your assistance!!!!!

    Thanks!!!!

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,523

    Re: Saving Attachment from Outlook to a specified location on my computer

    renaming the file .xlsx, does not make the file an excel workbook, to do that you would need to open in excel then saveAs workbook, by specifying the correct file format parameter
    this could be done within your code, but just get it to save first, when that works you can convert the saved file

    If olAtt.Filename = "_repairs.csv" Then
    of course while the attachment name may contain the string, it is never = to that, you should try instr to find if that string is contained within the name
    or you could try
    Code:
    If olAtt.Filename = format(date - 1,"m-d-yyyy") &  "_repairs.csv" Then
    though this would likely not work for monday
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •