Forum Discussion
Macro execute to loop all word doc files in a folder
- Oct 17, 2022
Also, to be on the safe side, I'd explicitly loop through Word documents, so that you don't get an error if the folder contains another type of file:
path = "C:\Users\hrhquek\Desktop\DEBT RECOVERY\Bizfile\" myFile = Dir(path & "*.doc*")
JMB17 , HansVogelaar ,
i think i know what was the issue. The 2 sub (extractdatafromwordtoexcel and findcopyandpaste) was taking reference from a range hence explains why there seems to be a duplicate data.
The macro works when i debug.print myFile.
However, i need to tweak the code for extracting data in a tables from a doc type document. Therefore, i believe the GetObject liner is throwing out issues. Is there a way to work around for this GetObject liner?
Dim path As String
Dim oDoc As Variant
Dim wordDoc As Object
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
path = "C:\Users\hrhquek\Desktop\DEBT RECOVERY\Bizfile\"
myFile = Dir(path & "*.doc*")
Do While myFile <> ""
Debug.Print myFile
Set oDoc = documents.Open(filename:=path & myFile)
Set wordDoc = GetObject(oDoc) '<-- I believe this is the error populating. Not able to take reference from a word document
With wordDoc
tbls = wordDoc.Tables.Count
If tbls = 0 Then
MsgBox "There no tables To extract"
End If
rowO = 6 'Extracting into cell A6
For tbBegin = 1 To tbls
With .Tables(tbBegin)
For RowNo = 1 To .Rows.Count
For ColNo = 1 To .Columns.Count
ws.Cells(rowO, ColNo) = Application.WorksheetFunction.Clean(.Cell(RowNo, ColNo).Range.Text)
Next ColNo
rowO = rowO + 1
Next RowNo
End With
rowO = rowO
Next tbBegin
End With
Call findcopyandpaste
'oDoc.Close
myFile = Dir
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
I'm not sure what the purpose of wordDoc is since you already have a reference to the document with oDoc?
GetObject accepts string argument(s), but you're passing it an object. Since error handling is set to 'resume next' you're not getting an error. If you want wordDoc to also reference the document (but is this really necessary - do you really need 2 references to the same object?), then
Set wordDoc = oDoc