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*")
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*")
, if i would like to add an additional file type such as docx; would the following be correct?
currently, nothing is being populated when the code is executed.
myFile = Dir(path & "*.docx*;*.doc*")- HansVogelaarOct 17, 2022MVP
"*.doc*" includes .doc, .docx and .docm
If the code doesn't process any documents, there must be something wrong with the path.
As JMB17 asked: "Is "Bizfile" the name of a folder, or is it the first part of the filename(s)?"
- hrh_dashOct 17, 2022Iron Contributor
alright, i thought i would need to include "doc" and "docx" file type to have the code running. ok, so right now, the folder has 2 word document files.
Currently when the macro is executed, data from the 1st word document file was populated twice rather then both word documents file being populated.
Attaching the screenshot below:
Data in the 1st and 2nd column has been removed due to confidentiality.
JMB17 , appreciate the response. Bizfile is folder.
The macro works after inputting the "\" after the Bizfile and include the "*.docx*" but somehow it resulted in duplicate entries.
- JMB17Oct 17, 2022Bronze Contributor
As Hans already stated, we don't know what's in the other procedures being called that extract the information. Specifically, we can't see how those procedures reference the word document referenced by the variable "oDoc" (because oDoc is a local variable and it's not being passed as an argument to the other procedures, so it's unclear how those procedures are referencing the document).
I'm assuming the word object variable itself is globally scoped - are the other procedures referencing the document by index number (documents(1))? If the first document is not closed (because the variable name "wordDoc" appears to be incorrect as Hans noted), then documents(1) would still be referencing the first document.
Personally, I would pass the document as an argument to the other function(s) instead of assuming which document it should process by some other means such as index number.
Macro1()
Macro2 oDoc
End Sub
Macro2(byref localVariableName as object)
debug.print localVariableName.Paragraphs.Count
End Sub
Also, you should consider adding Option Explicit to the top of your module, if you're not already using it. Then, the compiler will help catch variable name issues such as misspellings or if you accidentally used "wordDoc" when you meant "oDoc" (if worddoc is not module or global level).