http://expert.csdn.net/Expert/topic/2504/2504078.xml?temp=.6271021 xuminghua(企鹅) 大大作答如下:'================================================== Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Long Private Const BIF_RETURNONLYFSDIRS = &H1 Private Const BIF_NEWDIALOGSTYLE = &H40 Private Type BROWSEINFO 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'浏览文件夹函数 Public Function BrowseFolder(ByVal hwnd As Long, ByVal Title As String) As String Dim bi As BROWSEINFO Dim rtn, pid As Long Dim path As String * 512 Dim pos As Integer With bi .hOwner = hwnd .ulFlags = BIF_RETURNONLYFSDIRS Or BIF_NEWDIALOGSTYLE .lpszTitle = Title End With pid = SHBrowseForFolder(bi) rtn = SHGetPathFromIDList(ByVal pid, ByVal path) If rtn Then pos = InStr(path, Chr(0)) BrowseFolder = Left(path, pos - 1) Else BrowseFolder = "" End If End FunctionPrivate Sub Command1_Click() Text1.Text = BrowseFolder(Me.hwnd, "请选择您文件夹") End Sub '==================================================将最后的这个command1_click下的东西复制到你自己的按钮下,并把Text1改成你要保存返回路径的变量或者TextBox的名字。
使用这两个API Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Longmeteornk(reason for life) 贴的代码完全符合要求了
比如你用CommonDialog已经获得全路径,用FileSystemObject的getdrivename方法
xuminghua(企鹅) 大大作答如下:'==================================================
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Long
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_NEWDIALOGSTYLE = &H40
Private Type BROWSEINFO
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'浏览文件夹函数
Public Function BrowseFolder(ByVal hwnd As Long, ByVal Title As String) As String
Dim bi As BROWSEINFO
Dim rtn, pid As Long
Dim path As String * 512
Dim pos As Integer
With bi
.hOwner = hwnd
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_NEWDIALOGSTYLE
.lpszTitle = Title
End With
pid = SHBrowseForFolder(bi)
rtn = SHGetPathFromIDList(ByVal pid, ByVal path)
If rtn Then
pos = InStr(path, Chr(0))
BrowseFolder = Left(path, pos - 1)
Else
BrowseFolder = ""
End If
End FunctionPrivate Sub Command1_Click()
Text1.Text = BrowseFolder(Me.hwnd, "请选择您文件夹")
End Sub
'==================================================将最后的这个command1_click下的东西复制到你自己的按钮下,并把Text1改成你要保存返回路径的变量或者TextBox的名字。
CurrentProject.Path
'不带路径:
currentproject.Name
codeproject.Name
'带路径:
CodeDb.Name
Currentproject.FullName
不过记住currentproject.Name 和 codeproject.Name有时是不同的
打开VBA在立即窗口就可以用:?CurrentProject.Path 就可以查出当前路径了。
dir("yourpath+filename")是查看路径下的filename文件是否存在。
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Longmeteornk(reason for life) 贴的代码完全符合要求了