Consulting

Results 1 to 6 of 6

Thread: Error while saving .csv attachment in .xlsx format

  1. #1
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    3
    Location

    Question Error while saving .csv attachment in .xlsx format

    I get .csv report everyday through email so I coded VBA to save a copy to the folder. And I actually want it in .xlsx format and I'm trying to code the script able to change the format too.
    But the file can't be opened and error message pop up.

    "Excel cannot open the file 'filename' because the file format or file extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file"

    I actually hard coded to save as .xlsx but didn't change the format and that's why I'm having this error. I can think of a solution but that seems like there is a lot of redundancy.
    First save the .csv file and open that file with excel application. then saveAs .xlsx format. finally delete the .csv file.

    Please advise if there is any alternative.
    Appreciate that.

    Public Sub SaveAttachementAsXSLX(itm As Outlook.MailItem)
    
    
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim FileName As String
    
    
    saveFolder = "filePath"
         For Each objAtt In itm.Attachments
              objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName & ".xlsx"
              Set objAtt = Nothing
         Next
    
    
    End Sub

  2. #2
    You can't change a text format CSV file into an XML format Workbook by changing the extension. Do you need to convert it? Excel will open it in CSV format.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    3
    Location
    Yes I need to convert it and want to be done by VBA. How could I do that?

  4. #4
    You have already indicated in your original post how to do it. Open it with Excel and Save it.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    3
    Location
    Eeww such a simple/straightforward thought requires that much work. I'm just trying to figure a more efficient way.

    but thanks, I think I'm going to code it as my original way.

  6. #6
    This issue may occur when the Temporary Internet Files folder is stored on a server on which you do not have sufficient permissions.


Posting Permissions

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