2016-10-13 6 views
0

現在、ユーザーがファイルの宛先を選択できるダイアログボックスを設定しました。宛先が選択されたら、ファイルの名前を入力して、 "FileSaveName"をフルパスに設定します。C:\user\desktop\test.xml VBAを通じて提供されるこのツールを初めて使用しています。ファイル名と拡張子を別々に取得できますか?または文字列から文字を手動でトリミングする必要がありますか?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 
+0

のように行うことができ、このスレッドを参照してください。 http://www.thespreadsheetguru.com/the-code-vault/2014/3/2/retrieving-the-file-name-extension-from-a-file-path-string –

答えて

1

あなたは、Windowsスクリプティングオブジェクトを使用することができます。

Dim objFS as Object 
Dim objFile as Object 
Dim strPath as String 
Dim strFile as String 
Dim strExt as String 


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

    Set objFS = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFSO.GetFile(fileSaveName) 

    strFile = objFile.Name 
    strPath = objFile.ParentFolder 
    strExt = objFS.GetExtensionName(fileSaveName) 

    MsgBox "File path: " & strPath 
    MsgBox "File name: " & strFile 
    MsgBox "File extension: " & strExt 

    ' Remove references 
    Set objFile = Nothing 
    Set objFS = Nothing 


End If 

Good reference here

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 
関連する問題