简体   繁体   English

如何将 xlsm 保存为 xlsx?

[英]How to save xlsm as xlsx?

I have a xslm file.我有一个 xslm 文件。 I want to save the file as xlsx and email.我想将文件另存为 xlsx 和电子邮件。

I am able to SaveCopyAs it as xls file.我可以将它另存为 xls 文件。 If I try to save it as xlsx, it does get saved but when I open it, it gives an error.如果我尝试将它保存为 xlsx,它确实会被保存,但是当我打开它时,它会出错。

ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Path & "\MyFileName - " & Format(Date, "mm-dd-yyyy") & ".xlsx"

Excel cannot open the file '...path\\MyFileName.xlsx' because the file format or file extension is not valid. Excel 无法打开文件“...path\\MyFileName.xlsx”,因为文件格式或文件扩展名无效。 Verify that file has not been corrupted and that file extension matches the format of the file验证文件未损坏且文件扩展名与文件格式匹配

SaveCopyAs does not change the file-type. SaveCopyAs不会更改文件类型。

You simply cannot save a .xlsm as .xlsx via SaveCopyAs .您根本无法通过SaveCopyAs将 .xlsm 保存为 .xlsx 。

EDIT编辑

a workaround is to save a copy which then is changed in type while the old copy will be deleted like:一种解决方法是保存一个副本,然后更改类型,而旧副本将被删除,例如:

Dim wb As Workbook, pstr As String

pstr = ActiveWorkbook.Path & "\MyFileName - " & Format(Date, "mm-dd-yyyy") & ".xlsm"
ActiveWorkbook.SaveCopyAs Filename:=y

Set wb = Workbooks.Open(pstr)
wb.SaveAs Left(pstr, Len(pstr) - 1) & "x", 52
wb.Close False

Kill pstr

Try this:试试这个:

Sub SaveAsXLSX()
ThisWorkbook.Save   'Optional
Application.DisplayAlerts = False
ThisWorkbook.SaveAs ActiveWorkbook.Path & "\MyFileName - " & Format(Date, "mm-dd-yyyy"), 51 '51 = xlsx
Application.DisplayAlerts = True
ThisWorkbook.Close  'Optional
End Sub

All you need to do is SaveAs and change the file format to 51 ( xlsx )您需要做的就是SaveAs并将文件格式更改为51 ( xlsx )

If you want to "Save a copy" - SaveAs does practically the same thing - the difference being your currently open file becomes the saved file, but you can simply reopen the old one if you wish and nothing changes.如果您想“保存副本” - SaveAs几乎可以做同样的事情 - 不同之处在于您当前打开的文件成为保存的文件,但如果您愿意,您可以简单地重新打开旧文件,并且没有任何变化。

What you actually want to do is SaveAs a different file type, so use SaveAs .您真正想要做的是SaveAs一种不同的文件类型,因此请使用SaveAs

I This is more readable. I 这更具可读性。 TESTED.测试。

 Sub SaveXlsmAsXlsx() 
Dim wb As Workbook, Filenamepath As String, Filenameext As String, Filenameonly As String, Filepathonly As String

Application.DisplayAlerts = False
Filenamepath = ActiveWorkbook.FullName
Filenameext = ActiveWorkbook.Name
Filenameonly = Replace(Filenameext, ".xlsm", "")
Filepathonly = Replace(Filenamepath, ".xlsm", "")
Set wb = Workbooks.Open(Filenamepath)
'51 = xlsx
wb.SaveAs Filename:=Filepathonly & "_" & Format(Date, "mm-dd-yyyy"), FileFormat:=51
wb.Close True
'Kill- Best not to kill anyone, you might be sorry
ThisWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
  1. This code add to any module:此代码添加到任何模块:
Public Sub XLSMtoXLSX(FaylAdi As String)
Dim FullPath As String
Dim wb As Workbook

MsgBox "YOU WILL GET A WARNING AFTER COMPLETED, PLEASE WAIT"

ThisWorkbook.Save

On Error GoTo XETA

'You can change the name of the folder path below
FullPath = "C:\kohne sistem\Excel\VBA\Anbar\temp\" & FaylAdi & ".xlsm"
ThisWorkbook.SaveCopyAs FullPath

Application.DisplayAlerts = False
Set wb = Workbooks.Open(FullPath)
wb.SaveAs Left(FullPath, Len(FullPath) - 1) & "x", 51
wb.Close False
Kill FullPath
Application.DisplayAlerts = True
MsgBox "COMPLETED CORRECTLY"
Exit Sub
XETA: MsgBox "THERE WAS A FAULT SOMEWHERE"
End Sub
  1. Then you can use it like this:然后你可以像这样使用它:
Private Sub CommandButton1_Click()
Call XLSMtoXLSX(Date)
End Sub

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

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