简体   繁体   English

另存为.xls FileFormat:= 53时,我的Excel VBA-中突然出现错误

[英]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 对象'_Workbook'的运行时错误'1004'方法'SaveAs'失败

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 我正在运行MS Excel 2016版本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? 我想另存为.xls,以防万一我们将其发送到旧版本的Excel,我可以将其更改为.xlsx并重新编写一些代码,但我很好奇为什么突然出现此错误?

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 如果是隐藏文件问题,则可以利用FileSystemObject对象并循环,直到找到“免费”文件名

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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