如何通过编程来选择文件存放路径?
如何通过编程来选择文件存放路径?我想实现VBA编程实现VB6.0中目录列表控件dirlistbox控件的功能,可以实现对文件目录的选取,用户从而可以选择文件存放路径.不知VBA中有没有这种控件,如没有,有没有办法可以这种功能.请高手赐教!
回复
Public Sub Test_BrowseForFolder()<BR> Dim str As String '$<BR> str = ReturnFolder()<BR> MsgBox str<BR>End Sub 本帖最后由 作者 于 2005-3-28 7:45:45 编辑 <br /><br /> Joe Sutphin 写过下面这个用comdlg32.dll的SaveAs对话框替代
Option Explicit
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _<BR>"GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME<BR> lStructSize As Long<BR> hwndOwner As Long<BR> hInstance As Long<BR> lpstrFilter As String<BR> lpstrCustomerFilter As String<BR> nMaxCustFilter As Long<BR> nFilterIndex As Long<BR> lpstrFile As String<BR> nMaxFile As Long<BR> lpstrFileTitle As String<BR> nMaxFileTitle As Long<BR> lpstrInitialDir As String<BR> lpstrTitle As String<BR> flags As Long<BR> nFileOffset As Integer<BR> nFileExtension As Integer<BR> lpstrDefExt As String<BR> lCustdata As Long<BR> lpfnHook As Long<BR> lpTemplateName As String<BR>End Type
Public Function ShowSave(Filter As String, _<BR> InitialDir As String, _<BR> DialogTitle As String) As String<BR>Dim OFName As OPENFILENAME<BR> 'Set the structure size<BR> OFName.lStructSize = Len(OFName)<BR> 'set the owner window<BR> OFName.hwndOwner = 0<BR> 'set the filter<BR> OFName.lpstrFilter = Filter<BR> 'set the maximum number of chars<BR> OFName.nMaxFile = 255<BR> 'create a buffer<BR> OFName.lpstrFile = Space(254)<BR> 'create a buffer<BR> OFName.lpstrFileTitle = Space$(254)<BR> 'set the maximum number of chars<BR> OFName.nMaxFileTitle = 255<BR> 'set the initial directory<BR> OFName.lpstrInitialDir = InitialDir<BR> 'set the dialog title<BR> OFName.lpstrTitle = DialogTitle<BR> 'no extra flags<BR> OFName.flags = 0<BR> 'Show 'SaveAs File' dialog<BR> If GetSaveFileName(OFName) Then<BR> ShowSave = Trim(OFName.lpstrFile)<BR> Else<BR> ShowSave = ""<BR> End If<BR>End Function
Sub Test_SaveAs()<BR>Dim Filter As String<BR>Dim InitialDir As String<BR>Dim DialogTitle As String<BR>Dim FileSelected As String
Filter = "Drawing Files (*.dwg)" + Chr$(0) + "*.dwg" + Chr$(0) + _<BR> "All File(*.*)" + Chr$(0) & "*.*" + Chr$(0)<BR>InitialDir = "C:\Program Files\AutoCAD 2004\Sample"<BR>DialogTitle = "Save DWG as file"
FileSelected = ShowSave(Filter, InitialDir, DialogTitle)<BR>If FileSelected <> "" Then<BR> MsgBox "You just selected a file: " & FileSelected<BR> Else<BR> MsgBox "You didn't select a file."<BR>End If<BR>End Sub
页:
[1]