获取文件名称& VBA保存对话框的目的地

时间:2016-10-13 18:07:59

标签: excel vba excel-vba

我目前设置了一个对话框,允许用户选择文件目的地。选择目的地后,您可以输入文件的名称,并设置" FileSaveName"像C:\user\desktop\test.xml这样的完整路径我是VBA提供的这个工具的新手。是否可以单独获取文件名和扩展名?或者我需要手动修剪字符串中的字符吗?

fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="Text Files (*.xml), *.xml")
If fileSaveName <> False Then
    MsgBox "File path and file name: " & fileSaveName
    'MsgBox "File path: " & filepath
    'MsgBox "File name: " & filename
End If

2 个答案:

答案 0 :(得分:1)

您可以使用Windows Scripting对象:

AndroidManifest.xml

Good reference here

答案 1 :(得分:0)

你可以这样做

Sub main()
    Dim fileSaveName As Variant
    fileSaveName = Application.GetSaveAsFilename( _
    fileFilter:="Text Files (*.xml), *.xml")
    If fileSaveName <> False Then
        MsgBox "File path and file name: " & fileSaveName
        MsgBox "File path: " & GetFilePath(fileSaveName)
        MsgBox "File name: " & GetFileName(fileSaveName)
        MsgBox "File extension: " & GetFileExt(fileSaveName)
    End If

End Sub

Function GetFilePath(fileFullPath As Variant) As String
    GetFilePath = Left(fileFullPath, InStrRev(fileFullPath, "\"))
End Function

Function GetFileName(fileFullPath As Variant) As String
    GetFileName = Right(fileFullPath, Len(fileFullPath) - InStrRev(fileFullPath, "\"))
End Function

Function GetFileExt(fileFullPath As Variant) As String
    Dim fileName As String
    fileName = GetFileName(fileFullPath)
    GetFileExt = Right(fileName, Len(fileName) - InStrRev(fileName, "."))
End Function