【VB路径选择】VB 浏览文件夹对话框 选择文件保存路径
2011-06-17 15:07:06| 分类: 文件选择对话框 | 标签: |字号大中小 订阅
第一步:新建一个Modules,假设为Modulas1.bas
把下面的复制到Module.bas中
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const BIF_DONTGOBELOWDOMAIN = 2
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (LpBrowseInfo As BROWSEINFO) As Long
Public Declare Function SHGetPathFromIDlist Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public 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 GetFolder(ByVal hWnd As Long, Optional Title As String) As String
Dim bi As BROWSEINFO
Dim pidl As Long
Dim folder As String
folder = Space(255)
With bi
If IsNumeric(hWnd) Then .hOwner = hWnd
.ulFlags = BIF_RETURNONLYFSDIRS
.pidlroot = 0
If Title <> "" Then
.lpszTitle = Title & Chr$(0)
Else
.lpszTitle = "选择目录" & Chr$(0)
End If
End With
pidl = SHBrowseForFolder(bi)
If SHGetPathFromIDlist(ByVal pidl, ByVal folder) Then
GetFolder = Left(folder, InStr(folder, Chr$(0)) - 1)
Else
GetFolder = ""
End If
End Function
第二步:然后在你的窗体Form1中添加Command1的单击事件了
Private Sub Command1_Click()
Dim str
str = GetFolder(Me.hWnd, "浏览文件夹")
If str <> "" Then
Combo1.Text = str
Combo1.AddItem (str)
End If
End Sub
GetFolder函数返回文件夹的路径,比如你选择了System32文件夹,单击确定后,返回"c:\System32"
这样就实现了选择文件存储路径的问题了~
vb保存excel
想每次保存都自动重命名,如下次是002.xls,而不是覆盖以前的,
或者弹出一个重命名对话框也可以,我看了半天不会,希望高手解决!
代码说的详细点,少了我看不懂,哈哈
Private Sub CommandButton1_Click()
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\" & CStr(Val(Replace(ActiveWorkbook.FullName, ActiveWorkbook.Path & "\", "")) + 1) & ".xls"
End Sub
这样,你每次单击,就会在那个路径(也就是桌面,当然你可以第一次保存的时候选择别的路径,那么以后自动保存的时候就在那个路径)自动生成2.xls,3.xls…………我测试通过的。
==================
当然,你的原意肯定是不要按钮的,我这里只是提供一下测试,让你能够清晰的看到效果,你把我这个代码的核心
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\" & CStr(Val(Replace(ActiveWorkbook.FullName, ActiveWorkbook.Path & "\", "")) + 1) & ".xls"
相应的弄到你的代码里,就OK了。