简体   繁体   中英

Suddenly Getting an Error in my Excel VBA- when Saving as .xls FileFormat:=53

This code has been working for months, and today it is throwing the error:

Run-Time Error '1004' Method 'SaveAs' of Object '_Workbook' failed

The line of code causing the error is:

        '~~>. Save the file
        .SaveAs Filename:="\\MY\File\Path\Report_" & Format(Now(), "yyyymmdd") & ".xls", FileFormat:=53

I dont know what has changed here for this suddenly not to work, any ideas?

I am running MS Excel 2016, Version 1609

There is no file in that location with the same name.

I want to save as an .xls in case anyone we send this to has an older version of Excel, I could change it to an .xlsx and rewrite the code a bit but I am curious why all of the sudden this errored out?

Thanks for looking.

我认为要另存为“ .xls”,FileFormat应该为56 http://www.rondebruin.nl/win/s5/win001.htm

What you can do is to use the macro recorder for this. This is what mine produces:

ChDir "C:\Users\gropc\Desktop"
ActiveWorkbook.SaveAs Filename:="C:\Users\uname\Desktop\Book1.xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

The format should be taken into account.

if it's a hidden file issue you could exploit FileSystemObject object and loop until you find a "free" file name

so your code line could become the following:

.SaveAs fileName:=GetFreeFileName("\\MY\File\Path\Report_" & Format(Now(), "yyyymmdd"), ".xls")

which uses the following function:

Function GetFreeFileName(fileName As String, fileExt As String)
    Dim iFile As Long

    With CreateObject("FileSystemObject")
        Do While .FileExists(fileName & fileExt)
            iFile = iFile + 1
            fileName = fileName & "-" & iFile
        Loop
    End With
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM