VBA 常用的选择文件大总结

Private Sub CommandButton1_Click() 'shell 方法
        Set objShell = CreateObject("Shell.Application")
            Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)
                If Not objFolder Is Nothing Then
                    MsgBox objFolder.self.path
                End If
            Set objFolder = Nothing
        Set objShell = Nothing
End Sub
shell 方法
Private Sub CommandButton2_Click() 'FileDialog方法
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    If fd.Show = -1 Then MsgBox fd.SelectedItems(1)
    Set fd = Nothing
End Sub
FileDialog方法

 判断一个文件目录是否存在的简便方法

Public Sub 技巧18_004()
    Dim myFileName As String
    '指定带完整目录的文件夹名称
    myFileName = "H:\Excel\12 文件和文件夹操作\12 文件和文件夹操作1"
    If Not Len(Dir(myFileName, vbDirectory)) > 0 Then
        MsgBox "所指定的文件夹或文件不存在"
    End If
End Sub

 

posted on 2013-12-08 17:14  鱼东鱼  阅读(812)  评论(0编辑  收藏  举报

导航