VBA里如何调用目录对话框并取得路径

如题所述

第1个回答  2010-06-16
给你一个现成的,你只需拷贝到你的宏里去使用;
Sub 查找文件夹下子文件夹及其大小()
Dim theDir As String

Set pt = ActiveSheet.Range("a1")
pt.Worksheet.Columns(1).ClearContents
theDir = Application.InputBox("输入指定文件夹的路径:", "查看子文件夹及其大小")
pt = theDir
listPath theDir

pt.Worksheet.Columns("a:b").AutoFit
End Sub

Sub listPath(strDir As String)
Dim thePath As String
Dim strSdir As String
Dim theDirs As Scripting.Folders
Dim theDir As Scripting.Folder
Dim row As Integer
Dim s As String
Dim myFso As Scripting.FileSystemObject

Set myFso = New Scripting.FileSystemObject
If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
thePath = thePath & strDir

row = pt.row '此段为获取此目录下的文件名
s = Dir(thePath, 7) '获取第一个文件
Do While s <> ""
row = row + 1
Cells(row, 1) = s '文件的名称
Cells(row, 1).Font.Color = RGB(256, 12, 213)
Cells(row, 1).Font.Bold = Ture
s = Dir
Loop
Set pt = Cells(row, 1)
Set pt = pt.Offset(1, 0)
Set theDirs = myFso.getfolder(strDir).subfolders '取得所有子文件
For Each theDir In theDirs
pt = theDir.Path
pt.Next = theDir.Size
listPath theDir.Path
Next

Set myFso = Nothing
End Sub

Private Sub CommandButton1_Click()
查找文件夹下子文件夹及其大小
End Sub

参考资料:VBA,文件及文件夹目录

第2个回答  2010-06-14
'声明API函数
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As FolderInfor) As Long
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function EnableWindow Lib "user32" (ByVal hWnd As Long, ByVal fEnable As Long) As Long
'定义变量类型
Public Type FolderInfor
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Sub BrowDir()
Dim iFolder As FolderInfor
Dim pidl As Long, Flag As Long, iPath As String, Pos As Integer, myPath As String
'FindWindow取得Excel窗口的句柄
'在Excel窗口里禁止所有鼠标及键盘输入
EnableWindow FindWindow("XLMAIN", Application.Caption), False
'显示目录选择对话框
pidl = SHBrowseForFolder(iFolder)
'在Excel窗口里允许鼠标及键盘输入
EnableWindow FindWindow("XLMAIN", Application.Caption), True
iPath = Space$(512)
Flag = SHGetPathFromIDList(ByVal pidl, ByVal iPath)
If Flag Then
Pos = InStr(iPath, Chr$(0))
'取得选择的目录
myPath = Left(iPath, Pos - 1)
End If
MsgBox "你选择了 " & myPath
End Sub本回答被提问者采纳
第3个回答  2010-06-14
Option Explicit

Sub test()
MsgBox Application.ActiveWorkbook.Path
End Sub

vba如何打开文件夹对话框
1.打开一个表格文件;2.按组合按键【Alt+F11】调出VBA编辑器,插入宏取名为【打开文件选择对话框】;3.定义对话框变量(Dim fd As FileDialog:Set fd = Application.FileDialog(msoFileDialogFilePicker));4.设置文件默认目录及显示对话框;5.执行宏后就会弹出文件选择对话框。

VBA中怎么通过打开文件的对话框获取选择文件的路径并在其他模块中引用该...
1.插入一个模块或在已有的模块最上面一行定义一个全局变量 比如: Public iFileName As String 2.在这个工作薄的其他模块中都可以引用这个变量 比如:(先执行一次test1,再执行test2)Sub test1()'打开一个选择文件的对话框 iFileName = Application.GetOpenFilename End Sub Sub test2()If iFileName = ...

VB查找指定文件夹并获取文件路径的代码
1.首先打开Excel电子表格,然后在开发工具中打开VBA编辑器,如下图。2.在单元格区域输入一些内容,如下图。3.将模块插入到VBA编辑器中,如下图。4.文件的子访问路径()Dimfiless,tempfiless=Application。GetOpenFilename(,,“打开文件”)如果filess=False,那么MsgBox“你没有选择知道选择文件,程序...

Excel VBA和文件夹-1.7通过对话框灵活选定文件夹并打开对应文件_百度...
首先,我们创建一个对话框,让用户直接在VBA中选择所需的文件夹,无需预先指定路径,代码如下:对话框代码部分:这部分代码的作用是打开文件选择对话框:vb Sub OpenFolder()Dim fdlg As FileDialog Set fdlg = Application.FileDialog(msoFileDialogFolderPicker)If fdlg.Show = -1 Then '如果选择打开...

如何用VBA打开选择文件和选择路径的对话框
'在文件对话框对中,选择一个文件。Public Function ChooseOneFile(Optional TitleStr As String = 选择你要的文件, Optional TypesDec As String = 所有文件,Optional Exten As String = *.*) As String Dim dlgOpen As FileDialog Set dlgOpen = Application....

如何用VBA打开选择文件和选择路径的对话框
'在文件对话框对中返回选择一个文件夹的路径.Public Function ChooseFolder() As String Dim dlgOpen As FileDialog Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)With dlgOpen If .Show = -1 Then ChooseFolder = .SelectedItems(1)End IfEnd With Se...

excel vba 如何弹出选择路径的对话框并把用户选择的路径储存到变量
Set objFD = Application.FileDialog(msoFileDialogFolderPicker)With objFD If .Show = -1 Then ' 如果单击了确定按钮,则将选取的路径保存在变量中 sPath = .SelectedItems(1)End If End With

vba用FileDialog选择文件(夹)
打开对话框并等待用户选择文件。fd.show 获取用户选择的文件路径集合。dim filepaths set filepaths = fd.SelectedItems 遍历文件路径集合,获取每个文件的路径。for each path in filepaths print path next 至此,通过 VBA 的 FileDialog 函数,我们可以实现用户界面的文件选择功能,让程序更加友好易用。

VBA如何获取当前EXCEL文件的路径
1、首先打开Excel数据表,在开发工具中打开VBA编辑器。2、在单元格区域当中输入一些内容。3、在VBA编辑器中插入模块。4、Sub获取文件路径()Dim filess, tempfiless=Application.GetOpenFilename(, , "打开文件")If filess=False Then MsgBox "你未选择文件,程序即将退出!": Exit Subtemp=Split(...

VBA在使用用Application.GetOpenFilename时如何设定对话框打开路径?
替代方案如下:Sub Demo() With Application.FileDialog(msoFileDialogOpen) .InitialFileName = "D:\\" .Show End WithEnd Sub 如果实在要用 Application.GetOpenFilename 方法,可以在调用之前加两行代码,比如需要在打开前定位到的目录为 “D:\\123”,那么可以这样写:ChDrive "D:\\"ChD...

相似回答