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

时间:2018-07-06 18:11:55

标签: excel vba excel-vba

我的excel表格上有一个带有下面嵌入代码的按钮。基本上,我会尝试将工作表及其嵌入代码一起复制,因为工作表将部分提交,关闭,重新打开然后再完全提交。

如果我不关闭活动工作簿,则saveascopy函数在PartiallySubmit函数中可以正常工作,但是如果我关闭活动工作簿(ActiveWorkbook.Close),然后重新打开它,则会出现标题错误(错误: “ Excel无法打开文件'xxx.xlsm',因为文件格式或文件扩展名无效。请验证文件未损坏且文件扩展名与文件格式匹配。”)我想知道是否我需要在关闭代码之前在代码中的任何位置启用宏,以便在尝试重新打开宏时不会出现故障。

如果我没有在Partially_Submit函数中关闭工作簿,则FullySubmit函数可以保存副本并删除旧文件。如果将副本另存为.xls,则可以在兼容模式下将其关闭并重新打开。如果我将文件另存为其他文件(.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

1 个答案:

答案 0 :(得分:0)

似乎

FileFormat:=xlOpenXMLWorkbookMacroEnabled  

代替

FileFormat:=xlNormal 

足够了。