Recursive Word Extract

Post on 05-Dec-2015

216 views 1 download

description

vba code word extract

Transcript of Recursive Word Extract

Public c, xlC, xlR As IntegerPrivate Sub CommandButton1_Click()Dim fold As FolderDim fPath As Stringc = 2xlC = 1xlR = 2 Set fso = New FileSystemObject Set fold = fso.GetFolder(Application.ActiveWorkbook.Path) Sheet1.Range("A2:e1500").Value = ""CommandButton1.BackColor = vbRed Call extract(fold)CommandButton1.BackColor = vbGreenEnd SubPublic Function extract(f As Folder) Dim xl As Object Dim xl2 As Object Dim circuit, hyperlink As String Set xl = CreateObject("word.application") Set xl2 = CreateObject("excel.application") xl.documents.Add 'xl2.Workbooks.Add xl.Visible = True xl2.Visible = Truecircuit = f.PathSet SubFolders = f.SubFoldersstrFolders = "" For Each f In f.SubFolders extract f Next f For Each f In SubFolders On Error Resume Next strFolders = f.Name On Error Resume Next strFolders = strFolders & "\" On Error Resume Next 'Here put your path where you have your documents to read: myPath = circuit & "\" & strFolders 'End with '\' myFile = Dir(myPath & "*.docx") On Error Resume Next'Sheet1.Cells(c, 1) = myPath'Sheet1.Cells(c, 4) = myFilehyperlink = myPath & myFileSheet1.Hyperlinks.Add Anchor:=Sheet1.Cells(c, 5), Address:=hyperlink, TextToDisplay:=myFilec = c + 1On Error Resume Next Do While myFile "" On Error Resume Next xl.documents.Open Filename:=myPath & myFile, ReadOnly:=True xlTRC = 2 On Error Resume Next myCL = xl.ActiveDocument.Tables(1).Cell(3, 2).Range.Text On Error Resume Next 'xl2.ActiveWorkbook.ActiveSheet.Cells(xlR, 1) = myCL Sheet1.Cells(xlR, 1) = myCL On Error Resume Next myCL = xl.ActiveDocument.Tables(1).Cell(4, 2).Range.Text On Error Resume Next Sheet1.Cells(xlR, 2) = myCL On Error Resume Next myCL = xl.ActiveDocument.Tables(2).Cell(12, 2).Range.Text Sheet1.Cells(xlR, 3) = myCL On Error Resume Next myCL = xl.ActiveDocument.Tables(5).Cell(2, 1).Range.Text On Error Resume Next myCL = "" Do While xlTRC