VB-Word打开指定目录文件

Function GetAllFiles(ByVal path As String, ByVal results As Collection)
    Dim files As files
    Dim folder As folder
    Dim subFolders As Folders
    Dim file As file
    Dim t
    
    Set folder = Fso.GetFolder(path)
    Set subFolders = folder.subFolders()
    Set files = folder.files
    For Each file In files
        If Right(file.Name, 4) = ".doc" Then
            results.Add (path & "\" & file.Name)
        ElseIf Right(file.Name, 5) = ".docx" Then
            results.Add (path & "\" & file.Name)
        End If
    Next
    For Each t In subFolders
        Call GetAllFiles(path & "\" & t.Name, results)
    Next
End Function
Public Fso As Object
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'给指定目录下Word文件添加文档标识
Sub WordFlag2()
    Dim path As String
    Dim t
    Dim filePaths As New Collection
    
    Set Fso = CreateObject("Scripting.FileSystemObject")
    path = CreateObject("Shell.Application").BrowseForFolder(0, "请选择文件夹", 0, "").Self.path
    '得到所有文件路径存入集合中
    Call GetAllFiles(path, filePaths)
    For Each t In filePaths
        ShellExecute 0, "open", t, "", "", 1
    Next
End Sub

你可能感兴趣的:(VB)