如何在excel VBA中获取保存文件的路径和名称?

时间:2015-07-24 16:57:48

标签: excel-vba vba excel

我使用以下代码保存文件,但我无法获取保存的文件路径和名称。

有没有办法获取这些细节?

Sub Test3()

On Error Resume Next
Dim FileSelected As String
Dim strPath As String

FileSelected = Application.Dialogs(xlDialogSaveAs).Show

If Not FileSelected <> "False" Then
MsgBox "You have cancelled"
Exit Sub
End If

If FileSelected <> "False" Then
strPath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
    'displays the result in a message box
Call MsgBox(strPath, vbInformation, "Save Path")
Exit Sub
End If

End Sub

1 个答案:

答案 0 :(得分:0)

尝试使用Application.GetSaveAsFilename。创建Variant变量,并在保存后将其设置为Application.GetSaveAsFilename