【问题标题】:Excel VBA XLDialogSaveAs function not workingExcel VBA XLDialogSaveAs 函数不起作用
【发布时间】:2016-04-06 02:39:49
【问题描述】:

我正在尝试以 .xlsx 文件格式自动将 .xls 文件保存在硬编码位置。我希望 SaveAs 对话框显示硬编码的位置,以及在“文件名:”字段中编码的文件名。因此,我需要做的就是单击“保存”按钮。

但是,当我想将文件保存在 H 盘中时,“另存为”对话框总是显示 C 盘。

以下是我的代码:

Option Explicit

Sub externalRatingChangeFile()

    'Declare the data type of the variables
    Dim wks As Worksheet
    Dim sFilename As String

    'Set wks to the current active worksheet
    Set wks = ActiveWorkbook.ActiveSheet

    'Set the location to save the file to a variable
    sFilename = "H:\testing file"

    'Save as .xlsx file in the specific location stated earlier
    'If there are errors in the code, set wks to nothing and end the process
    On Error GoTo err_handler
    ChDrive sFilename
    ChDir sFilename
    Application.Dialogs(xlDialogSaveAs).Show (sFilename & "\TestingFile - " & Format(Date, "YYYYMMDD") & ".xlsx")

    'System to/not display alerts to notify Users that they are replacing an existing file.
    Application.DisplayAlerts = True

    err_handler:
    'Set Wks to its default value
    Set wks = Nothing

End Sub

【问题讨论】:

  • 您是否要保存工作簿或工作表? Dialogs(xlDialogSaveAs) 只会在先前未保存工作簿时在初始文件夹中启动。
  • 我正在尝试保存工作簿。该工作簿是从在线资源中导出的,之前没有保存在任何文件夹中。这就是 SaveAs 对话框一直向我显示 C 驱动器的原因吗?

标签: vba excel


【解决方案1】:

不显示另存为对话框,直接保存到文件夹。

   Application.DisplayAlerts = False
   wks.SaveAs (sFilename + "\TestingFile - " + Format(Date, "YYYYMMDD") + ".xlsx")
   Application.DisplayAlerts = True

   Application.DisplayAlerts = False
   wks.SaveCopyAs (sFilename + "\TestingFile - " + Format(Date, "YYYYMMDD") + ".xlsx")
   Application.DisplayAlerts = True

最后,您可以创建自己的对话框以确保保存在正确的位置:

'Result = 2 is Cancel
'Result = 1 is Ok
result = MsgBox("Would You Like To Save in the Following Location: " + "H:\Test File....", vbOKCancel, "Save As")

【讨论】:

  • 嗨 Tyler,我希望在保存之前能够看到我正在使用正确的文件名将文件保存在正确的位置。还有其他方法可以显示对话框吗?
  • 您可以使用 msgbox 代替。这可以作为一个对话框。
  • @Jeeped 下面有一个适合您的解决方案。
【解决方案2】:

虽然我更喜欢Application.GetSaveAsFilename method(请参阅this),但如果之前未保存原始工作簿,则在 xlDialogSaveAs 上设置初始文件夹应该没有问题。

Sub externalRatingChangeFile()
    Dim bSaved As Boolean
    Dim xlsxFileFormat As XlFileFormat

    'Declare the data type of the variables
    Dim wks As Worksheet
    Dim sFilename As String

    'Set wks to the current active worksheet
    Set wks = ActiveWorkbook.ActiveSheet

    'Set the location to save the file to a variable
    sFilename = "H:\testing file"
    xlsxFileFormat = XlFileFormat.xlOpenXMLWorkbook

    'Save as .xlsx file in the specific location stated earlier
    On Error GoTo err_handler
    bSaved = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=sFilename & "\TestingFile - " & Format(Date, "YYYYMMDD"), _
                                                      arg2:=xlsxFileFormat)

    'System to/not display alerts to notify Users that they are replacing an existing file.
    Application.DisplayAlerts = True

err_handler:
    'Set Wks to its default value
    Set wks = Nothing

End Sub

【讨论】:

  • @JJ2015 您的文件夹路径是否存在。这种方法对我有用,但要求路径在保存之前实际存在。否则它将恢复到默认路径。
  • 我实际上将它运行到了我的 T: 驱动器(插入我的笔记本电脑的 SD 卡),只要它是一个新的工作簿,它就可以正常运行。您确定 H:\testing file 文件夹存在吗?如果不是,则默认为 C:。
  • @TylerFurrer & Jeeped,现在可以了!我再次遇到该错误,因为我意识到我正在从保存在桌面上的文件中进行测试。但现在一切都很好。非常感谢你们!
猜你喜欢
  • 1970-01-01
  • 2014-04-02
  • 2012-07-25
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多