zoukankan      html  css  js  c++  java
  • 20171022xlVBA练手提取入所记录

    Sub GetWordText改进()
        Dim Wb As Workbook
        Dim Sht As Worksheet
        Dim Rng As Range
        
        Dim wdApp As Object
        Dim wdDoc As Object
        Dim FilePaths
        Dim FilePath
        Dim Arr(1 To 10000, 1 To 6)
        Dim n As Long
        Dim Index As Long
        
        Dim Regex As Object
        Dim Mh As Object
        Pattern = ".*?[::](S*)s*?.*?[::](S*)s*?" & _
            ".*?[::](S*)s*?.*?[::](S*)s*?" & _
            ".*?[::](S*)s*?.*?[::](S*)"
        Set Regex = CreateObject("VBScript.RegExp")
        With Regex
            .Global = True
            .Pattern = Pattern
        End With
    
        Set Wb = Application.ThisWorkbook
        Set Sht = Wb.Worksheets("汇总")
        With Sht
            .UsedRange.Offset(1).ClearContents
        End With
        FilePaths = FsoGetFiles(Wb.Path & "", "*.doc*")
        If FilePaths(1) = "None" Then Exit Sub
        Index = 0
        
        
        Set wdApp = CreateObject("Word.Application")
        For n = LBound(FilePaths) To UBound(FilePaths)
            
            'On Error Resume Next
            Set wdDoc = wdApp.documents.Open(FilePaths(n))
            If wdDoc Is Nothing Then
                GoTo NextDocument
            Else
                If wdDoc.Tables.Count > 0 Then
                    Debug.Print "含表格:"; FilePaths(n)
                    Index = Index + 1
                    For j = 1 To 6
                        Text = wdDoc.Tables(1).cell(1, j).Range.Text
                        Text = Replace(Text, Chr(10), "")
                        Text = Replace(Text, Chr(7), "")
                        Text = Replace(Text, Chr(13), "")
                        Arr(Index, j) = "'" & Text
                        Debug.Print Index; "     "; Arr(Index, j)
                    Next j
                Else
                    Debug.Print "纯文本:"; FilePaths(n)
                    If Regex.test(wdDoc.Content.Text) Then
                        Set Mh = Regex.Execute(wdDoc.Content.Text)
                        Index = Index + 1
                        For j = 0 To Mh.Item(0).submatches.Count - 1
                            Arr(Index, j + 1) = "'" & Mh.Item(0).submatches(j)
                            Debug.Print Index; "     "; Arr(Index, j + 1)
                        Next j
                    End If
                End If
            End If
            wdDoc.Close False
    NextDocument:
            On Error GoTo 0
        Next n
        
        wdApp.Quit
        
        
        With Sht
            Set Rng = .Range("A2")
            Set Rng = Rng.Resize(UBound(Arr), UBound(Arr, 2))
            Rng.Value = Arr
        End With
        
        
        Set Wb = Nothing
        Set Sht = Nothing
        Set Rng = Nothing
        Set wdApp = Nothing
        Set wdDoc = Nothing
        
    End Sub
    Function FsoGetFiles(ByVal FolderPath As String, ByVal Pattern As String, Optional ComplementPattern As String = "") As String()
        Dim Arr() As String
        Dim FSO As Object
        Dim ThisFolder As Object
        Dim OneFile As Object
        ReDim Arr(1 To 1)
        Arr(1) = "None"
        Dim Index As Long
        Index = 0
        Set FSO = CreateObject("Scripting.FileSystemObject")
        On Error GoTo ErrorExit
        Set ThisFolder = FSO.getfolder(FolderPath)
        If Err.Number <> 0 Then Exit Function
        For Each OneFile In ThisFolder.Files
            If OneFile.Name Like Pattern Then
                If Len(ComplementPattern) > 0 Then
                    If Not OneFile.Name Like ComplementPattern Then
                        Index = Index + 1
                        ReDim Preserve Arr(1 To Index)
                        Arr(Index) = OneFile.Path '& OneFile.Name
                    End If
                Else
                    Index = Index + 1
                    ReDim Preserve Arr(1 To Index)
                    Arr(Index) = OneFile.Path '& OneFile.Name
                End If
            End If
        Next OneFile
    ErrorExit:
        FsoGetFiles = Arr
        Erase Arr
        Set FSO = Nothing
        Set ThisFolder = Nothing
        Set OneFile = Nothing
    End Function
    

      

  • 相关阅读:
    luogu P5488 差分与前缀和 FFT
    luogu P4173 残缺的字符串 FFT
    《数据结构与算法分析(C++语言描述)》
    《C语言—从入门到项目实践》Issue分析及总结
    操作系统学习笔记——第六章 文件管理
    操作系统学习笔记——第五章 I/O设备管理
    操作系统学习笔记——第四章 存储管理
    操作系统学习笔记——第二章 进程管理 和 第三章 死锁
    操作系统学习笔记——第一章 操作系统概述
    操作系统学习笔记——全部知识点流程图
  • 原文地址:https://www.cnblogs.com/nextseven/p/7712066.html
Copyright © 2011-2022 走看看