从FileDialog获取的字符串不会显示在消息框中

时间:2016-07-07 23:13:56

标签: excel vba

我正在尝试使用以下代码显示文件保存在消息框中的位置:

Sub export()
Dim MyPath As String
Dim MyFileName As String

MyFileName = "MyFileName"

Worksheets("Tab").Copy

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = "Path"
    If .Show = -1 Then
        GoTo Nextcode1
    Else
        GoTo Nextcode2
    End If
    MyPath = .SelectedItems(1) & "\"

End With


Nextcode1:
Block of codes that deals with existing file name.
GoTo Nextcode3

Nextcode2:
Block of codes that deals with cancel.
GoTo Nextcode4

NextCode3:
Application.DisplayAlerts = False
With ActiveWorkbook
    .SaveAs fileName:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
End With
Application.DisplayAlerts = True
Worksheets("OtherTab").Activate
MsgBox ("The tab has been exported to " & MyPath & MyFileName & ".")
GoTo NextCode4

NextCode4:
End Sub

但是,消息框仅显示

The tab has been exported to MyFileName.

完全省略MyPath。我尝试了以下代码

PathName = MyPath & MyFileName
MsgBox ("The tab has been exported to " & PathName & ".")

Cstr(MyPath)
MsgBox ("The tab has been exported to " & MyPath & MyFileName & ".")

无济于事。我怀疑从msoFileDialogFolderPicker获得的路径名不是字符串对象,但我不知道如何处理它。感谢帮助!

1 个答案:

答案 0 :(得分:1)

好吧,我的坏。在

MyPath = .SelectedItems(1) & "\"

行应该在

之下
If .Show = -1 Then