vba 如何保存在另存为对话框中选择的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29522278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save a file selected in Save As dialog?
提问by Rick
I found code online that opens a Save As dialog to a location on a drive.
我在网上找到了一个代码,它打开一个另存为对话框到驱动器上的某个位置。
When you click "save" the file does not save.
当您单击“保存”时,文件不会保存。
Dim varResult As Variant
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
InitialFileName:="\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
Exit Sub
End If
回答by Soulfire
You have to actually explicitly tell Excel to save the workbook.
您必须实际明确地告诉 Excel 保存工作簿。
Sub Mac2()
Dim varResult As Variant
Dim ActBook As Workbook
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
"Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
InitialFileName:="\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal
Exit Sub
End If
End Sub
Using the GetSaveAsFilenameonly gets the path of the file to save, whereas the SaveAsmethod actually saves the workbook.
使用GetSaveAsFilenameonly 获取要保存的文件的路径,而该SaveAs方法实际上是保存工作簿。
Upon some consideration, I might suggest using the SaveCopyAsmethod instead of simply SaveAs. As the name suggests, this will leave your original workbook in tact and save off a copy. To do this is a rather simply modification.
经过一些考虑,我可能会建议使用SaveCopyAs方法而不是简单的 SaveAs。顾名思义,这将使您的原始工作簿保持原样并保存副本。要做到这一点是一个相当简单的修改。
You would replace
你会替换
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal
With
和
ActiveWorkbook.SaveCopyAs Filename:=varResult
One final consideration I would add is that if you save your macro-enabled workbook as a .xlsx (either by SaveAs or the SaveCopyAs) then you will lose the macros, either in your original workbook if you use SaveAs or in the copy that is saved if you use SaveCopyAs. I would consider saving the file as a .xlsm instead, if you need macros to be available.
我要补充的最后一个考虑是,如果您将启用宏的工作簿另存为 .xlsx(通过 SaveAs 或 SaveCopyAs),那么您将丢失宏,无论是在您使用 SaveAs 的原始工作簿中,还是在使用 SaveAs 的副本中如果您使用 SaveCopyAs,则保存。如果您需要宏可用,我会考虑将文件另存为 .xlsm。
回答by TimAi
I prefer to use the shortest code:
我更喜欢使用最短的代码:
Application.Dialogs(xlDialogSaveAs).Show ("c:\my_folder\")
It's the standard Excel save dialog.
这是标准的 Excel 保存对话框。
It has several parameters (not named), you may need them:
它有几个参数(未命名),您可能需要它们:
Dim strFilename As String: strFilename = "report1"
Dim strFolder As String: strFolder = "C:\temp\" 'initial directory - NOTE: Only works if file has not yet been saved!
Dim xlfFileFormat As XlFileFormat: xlfFileFormat = XlFileFormat.xlOpenXMLWorkbook 'or replace by other XlFileFormat
Dim strPassword As String: 'strPassword = "password" 'The password with which to protect the file - if any
Dim booBackup As Boolean: 'booBackup = True '(Whether to create a backup of the file.)
Dim strWriteReservationPassword As String: 'strWriteReservationPassword = "password2" ' (The write-reservation password of the file.)
Dim booReadOnlyRecommendation As Boolean: booReadOnlyRecommendation = False '(Whether to recommend to the user that the file be opened in read-only mode.)
Dim booWorkbookSaved As Boolean ' true if file saved, false if dialog canceled
If Len(strFolder) > 0 Then ChDir strFolder
booWorkbookSaved = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=strFilename, Arg2:=xlfFileFormat, Arg3:=strPassword, _
Arg4:=booBackup, Arg5:=strWriteReservationPassword, Arg6:=booReadOnlyRecommendation)

