登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

凌晨阳光

 
 
 

日志

 
 

VBA 打开选择文件和目标文件夹对话框  

2011-01-20 12:00:11|  分类: Excel自动化 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

一、选择文件

(一)、GetOpenFilename

适用于OFFICE2000及以上版本

 expression.GetOpenFilename(FileFilter, FilterIndex, Title, ButtonText, MultiSelect)

1、单一文件

Sub getfile()

    fileToOpen = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
    If fileToOpen <> False Then
   
    Else
       
    End If

End Sub

2、多个文件

Sub getfiles()
Dim filetoopen As Variant
Dim i As Integer

    filetoopen = Application.GetOpenFilename("Excel Files (*.xls), *.xls", Title:="选择文件", MultiSelect:=True)
    If Not IsArray(filetoopen) Then
       
    Else
        For i = LBound(filetoopen) To UBound(filetoopen)
           
        Next
    End If

End Sub

(二)、Application.FileDialog(不适用于OFFICE2000

Sub getfile()

Dim lngCount As Long

    With Application.FileDialog(msoFileDialogFilePicker )
        .AllowMultiSelect = True
        .Show
        For lngCount = 1 To .SelectedItems.Count
            MsgBox .SelectedItems(lngCount)
        Next lngCount
    End With   
End Sub

二、调用目标文件夹

一)、Application.FileDialog

不适用于OFFICE2000

Sub getfolder()
Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        .Title = "选择文件夹"
        If .Show = -1 Then
            msgbox  .SelectedItems(1)
        End If
    End With
End Sub

 

OFFICE2000可用Shell来调用

Sub Sample2()
     Dim Shell, myPath
     Set Shell = CreateObject("Shell.Application")
     Set myPath = Shell.BrowseForFolder(0, "请选择文件夹", 0, 0)
     If Not myPath Is Nothing Then MsgBox myPath.self.Path
     Set Shell = Nothing
     Set myPath = Nothing
End Sub 

注:BrowseForFolder 方法
显示对话框由用户选择目录,可根据需求设定允许用户建立目录

语法
strDir = Shell.BrowseForFolder([varinitDir], [varTitle], [varDescription], [varNew])

参数
strDir
返回用户选择的目录名称,取消操作时为空
varinitDir
初始化目录,显示对话框时将自动定位到给定的目录
varTitle
对话框标题
varDescription
对话框描述
varNew
是否允许用户创建目录

 

详细资料可以参考:http://wenku.baidu.com/view/dbc899254b35eefdc8d3331c.html

  评论这张
 
阅读(6836)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018