vba 如何使用VBA获取文本框中的浏览文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13080570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the browse file path in text box using VBA?
提问by user441978
How to get the browse file name into text box ? if get the file path, how to split the file name?
如何将浏览文件名放入文本框中?如果获取文件路径,如何拆分文件名?
I tried application.GetOpenFilename("Text Files(*.txt),*.txt")
我试过 application.GetOpenFilename("Text Files(*.txt),*.txt")
Please advise to display into the text box and how to split the exact file name only to read the text file?
请建议显示到文本框中以及如何拆分确切的文件名仅读取文本文件?
回答by Jean-Fran?ois Corbett
Don't waste your time reinventing the wheel: the FileSystemObjectwill do this for you.
不要浪费时间重新发明轮子:FileSystemObject会为您完成这项工作。
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Sheet1.TextBox1.Text = FSO.GetFilename("C:\mydir\myfile.dat")
The textbox now contains the text myfile.dat
.
文本框现在包含文本myfile.dat
。
回答by Dick Kusleika
The Dir function will give you the file name as long as it's a file that exists - and yours will be if you use GetOpenFilename.
只要文件存在,Dir 函数就会为您提供文件名 - 如果您使用 GetOpenFilename,您的文件名也会如此。
Sub GetFileName()
Dim sFullName As String
Dim sFileName As String
sFullName = Application.GetOpenFilename("*.txt,*.txt")
sFileName = Dir(sFullName)
Debug.Print sFullName, sFileName
End Sub
回答by Dave Lewis
Here is a VBA routine to return the file name stripped of its path. Its easily modified to return the path instead, or both.
这是一个 VBA 例程,用于返回去除其路径的文件名。它很容易修改为返回路径,或两者兼而有之。
'====================================================================================
' Returns the file name without a path via file open dialog box
'====================================================================================
' Prompts user to select a file. Which ever file is selected, the function returns
' the filename stripped of the path.
Function GetAFileName() As String
Dim someFileName As Variant
Dim folderName As String
Dim i As Integer
Const STRING_NOT_FOUND As Integer = 0
'select a file using a dialog and get the full name with path included
someFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If someFileName <> False Then
'strip off the folder path
folderName = vbNullString
i = 1
While STRING_NOT_FOUND < i
i = InStr(1, someFileName, "\", vbTextCompare) 'returns position of the first backslash "\"
If i <> STRING_NOT_FOUND Then
folderName = folderName & Left(someFileName, i)
someFileName = Right(someFileName, Len(someFileName) - i)
Else 'no backslash was found... we are done
GetAFileName = someFileName
End If
Wend
Else
GetAFileName = vbNullString
End If
End Function
回答by Alex K.
Easiest way is to simply read from the final "\"
;
最简单的方法是简单地从 final 读取"\"
;
tbx.text = mid$(someFileName, 1 + InStrRev(someFileName, "\"), Len(someFileName))
回答by Alex K.
Button1 click
Button1 点击
OpenFileDialog1.ShowDialog()
Me.TextBox1.Text = OpenFileDialog1.FileName
End Sub
Textbox1 change
文本框 1 更改
Dim File As System.IO.FileInfo
File = My.Computer.FileSystem.GetFileInfo(TextBox1.Text)
Dim Path As String = File.DirectoryName
TextBox2.Text = Path
Dim fileName As String = File.Name
TextBox3.Text = fileName
End Sub