如何将xlsm转换为xlsx保持相同的名称

时间:2017-11-29 20:05:01

标签: excel vba excel-vba

如何将格式从xlsm更改为xlsx。我需要一个脚本来保存带有xlsm扩展名的xlsx文件的副本,但不要求用户输入文件名,它需要保留原始名称,唯一的是用户需要做的是选择保存文件的位置。

Sub changeext()

Dim s_as As String

s_as = ThisWorkbook.FullName

s_as = Left(s_as, InStrRev(s_as, ".") - 1) & ".xlsx"

ThisWorkbook.SaveAs FileName:=s_as

Application.DisplayAlerts = False

End Sub

2 个答案:

答案 0 :(得分:1)

我认为您只想添加.xlxs副本而不是在启用宏的工作簿上写入。尝试下面的代码行,它应该工作,把你的工作簿名称放在“把工作簿名称放在这里”,打开的文件将是.xlsx文件。

Application.ScreenUpdating = False
Application.DisplayAlerts = False

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & "Put Workbook Name here" & ".xlsx", FileFormat:=51, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Application.DisplayAlerts = True
Application.ScreenUpdating = True

答案 1 :(得分:1)

您需要创建FolderPicker功能:

Sub changeext()
Dim objFolder As Object, objFSO As Object

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(ChooseFolder)

ThisWorkbook.SaveCopyAs Filename:=objFolder & "\" & Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & "xlsx"

End Sub

Function ChooseFolder() As String
Dim fldr As FileDialog
Dim sItem As String

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder to save down the copy of this workbook"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With

NextCode:
    ChooseFolder = sItem
    Set fldr = Nothing
End Function