简体   繁体   English

错误:重新打开带有嵌入式VBA代码的xlsm文件时,“ Excel无法打开文件'xxx.xlsm',因为文件格式无效。”

[英]Error: “Excel cannot open the file 'xxx.xlsm' because the file format is not valid.” when re-opening a xlsm file with embedded VBA code

I have a button on my excel sheet with the embedded code below. 我的excel工作表上有一个带有以下嵌入式代码的按钮。 Basically, I am trying to copy the sheet along with the embedded code since the sheet will be partially submitted, closed, re-opened, and then fully submitted. 基本上,我会尝试将工作表及其嵌入代码一起复制,因为工作表将部分提交,关闭,重新打开然后再完全提交。

The saveascopy function works perfectly in the PartiallySubmit function if I do not close the active workbook, but if I close the active workbook (ActiveWorkbook.Close) and then re-open it, I get the error in the Title (Error: "Excel cannot open the file 'xxx.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.") I'm wondering if I need to enable macros anywhere in the code before closing it so it does not malfunction when trying to re-open. 如果我没有关闭活动工作簿,则saveascopy函数在PartiallySubmit函数中可以正常工作,但是如果我关闭活动工作簿(ActiveWorkbook.Close),然后重新打开它,则会出现标题错误(错误:“ Excel无法打开文件“ xxx.xlsm”,因为文件格式或文件扩展名无效。请确认文件未损坏且文件扩展名与文件格式匹配。”)我想知道是否需要启用在关闭代码之前,请在代码中的任意位置使用宏,这样在尝试重新打开时它不会出现故障。

If I do not close the workbook in the Partially_Submit function, then the FullySubmit function works to save a copy and delete the old file. 如果我没有在Partially_Submit函数中关闭工作簿,则FullySubmit函数可以保存副本并删除旧文件。 If I save the copy as a .xls, I am able to close and re-open it in compatibility mode. 如果将副本另存为.xls,则可以在兼容模式下将其关闭并重新打开。 If I save the file as any other (.xlsx or .xlsm) I get the same error ("Excel cannot open the file 'xxx.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.") 如果我将文件另存为其他文件(.xlsx或.xlsm),则会收到相同的错误(“ Excel无法打开文件'xxx.xlsm',因为文件格式或文件扩展名无效。请验证文件是否为损坏,并且文件扩展名与文件格式匹配。”)

Sub Partially_Submit()

    Dim Path, Filename As String


    Path = "\\aaa\bbb\ccc\"

    Filename = ActiveSheet.Name _
    & "_" _
    & ActiveSheet.Cells.Find(What:="Date:", 
    LookIn:=xlValues, Lookat:=xlWhole).Offset(0, 1) _
    & "_" _
    & ActiveSheet.Cells.Find(What:="S/N:", LookIn:=xlValues, 
    Lookat:=xlWhole).Offset(0, 1) _
    & "_" _
    & ActiveSheet.Cells.Find(What:="FRI#:", LookIn:=xlValues, 
    Lookat:=xlWhole).Offset(0, 1) _

    Application.ScreenUpdating = False

'Move to New Workbook

    ActiveSheet.Unprotect
    On Error Resume Next
        ActiveSheet.ShowAllData
    On Error GoTo 0

    ActiveSheet.Copy

    ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xlsm", 
    FileFormat:=xlNormal

    Application.DisplayAlerts = False

    **ActiveWorkbook.Close**

    Application.DisplayAlerts = False
    Application.DisplayAlerts = True

    'Restore User View
    Application.DisplayFormulaBar = True
    With ActiveWindow
        .DisplayHorizontalScrollBar = True
        .DisplayWorkbookTabs = True
        .DisplayHeadings = True
    End With

    Application.ScreenUpdating = False

End Sub


Sub FullySubmit()

    Dim Path, NewPath As String, OldFile As String

    Path = "\\aaa\bbb\ccc\"

    Filename = ActiveSheet.Name _
    & "_" _
    & ActiveSheet.Cells.Find(What:="Date:", LookIn:=xlValues, 
    Lookat:=xlWhole).Offset(0, 1) _
    & "_" _
    & ActiveSheet.Cells.Find(What:="S/N:", LookIn:=xlValues, 
    Lookat:=xlWhole).Offset(0, 1) _
    & "_" _
    & ActiveSheet.Cells.Find(What:="FRI#:", LookIn:=xlValues, 
    Lookat:=xlWhole).Offset(0, 1) _
    & "_" _
    & ActiveSheet.Cells.Find(What:="Completion Date:", 
    LookIn:=xlValues, Lookat:=xlWhole).Offset(0, 1) _

    NewPath = "\\aaa\bbb\ccc\ddd\"

    OldFile = ActiveWorkbook.FullName

    'ActiveWorkbook.SaveAs Filename:=NewPath & Filename & ".xlsm", 
    FileFormat:=xlNormal

    ActiveWorkbook.SaveAs Filename:=NewPath & Filename & ".xls", 
    FileFormat:=xlNormal

    Kill OldFile

End Sub

It seems 它似乎

FileFormat:=xlOpenXMLWorkbookMacroEnabled  

instead of 代替

FileFormat:=xlNormal 

was adequate. 足够了。

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

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