|
|
Thread Tools | Display Modes |
#1
|
||||
|
||||
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
Hi,
I am a novice VBA user. I have about 300 files I need to process. I would like to apply a macro to all the files. When the Files are in a standard folder I can use the macro below. It works perfectly. It finds the files of a .docx type and applies a macro to it. I now need help with a macro that will go through all the subfolders in the directory and apply my macro. I have tested many versions of loop through directory VBA codes and have had no luck in getting the codes to work. Sub ApplyMacroToAllFiles() Dim file Dim path As String path = "C:\Users\\Desktop \Folder\" file = Dir(path & "*.docx") Do While file <> "" Documents.Open FileName:=path & file Call RemoveallBoldWords ActiveDocument.Save ActiveDocument.Close ' Set file to next in Dir file = Dir() Loop End Sub I simply need to be able to process the documents in the sub folders. If any one can help me I would be so grateful. I have spent 2 days testing different VBA directory loop codes from the internet with no success. I get run time errors, no response from the macro. It's really frustrating. Thank you in advance J |
#2
|
||||
|
||||
Take a look at http://www.gmayor.com/document_batch_processes.htm which will do all the background folder handling. If you modify your RemoveAllBoldWords macro to follow the example macro format, it will run the macro as a custom process on the documents as they are opened.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
Hi Graham,
thank you for your help let me test this. J |
#4
|
||||
|
||||
Hi Graham,
it gives me the following error http://tinypic.com/r/2s12djd/8 I have inserted my macro into normal template modules http://tinypic.com/r/14b13jt/8 How do I solve this thank you J Last edited by jc491; 09-11-2015 at 05:15 AM. Reason: Image not showing |
#5
|
|||
|
|||
I've not looked at Graham's code in awhile, but we worked on this together. I'm pretty sure that you will need to change your Sub to a Function in this format:
Code:
Function FontChangeArial16Blue(ByRef oDoc As Word.Document) As Boolean On Error GoTo Err_Handler With oDoc.Range.Font .Size = 16 .Name = "Arial Unicode MS" .Color = wdColorBlue End With FontChangeArial16Blue = True lbl_Exit: Exit Function Err_Handler: FontChangeArial16Blue = False Resume lbl_Exit End Function |
#6
|
||||
|
||||
Thanks Greg,
for the code. Ive already become nervous looking at the code. Do I just paste my macros in between the function. Does it mean I have to change all my macros to a function? I simply use the macro recorder to record functions. Grahams Batch auto states to simply select the macro name. http://tinypic.com/r/aw2vjc/8 J |
#7
|
|||
|
|||
J,
I think if you read Graham's web page carefully you will see that the UserDefined processes have to be a Function that takes a document object (e.g., oDoc) as an argument and return a Boolean value. His add-in, like mine, does the part of looping through folders and sub-folders looking for documents. When a document is found, it is opened and then passed as an argument to the UserDefined function. The function does the work on the document and reports back success or failure. On success the document is saved and closed. On failure it is simply closed. I'm not sure what all your macros do, but you certainly wouldn't paste any code that opens, saves or closes the document. Try replacing my ... With oDoc.Font ... End With code with your code (less any open, save or close steps) and see what happens. |
#8
|
||||
|
||||
Hi Greg,
oh thank you that explains a lot. I was not sure about the function bit. if its not too irritating a question to ask - where exactly do put my functions in a module? I will fiddle about with this and report back j |
#9
|
||||
|
||||
Greg,
thank you so much for your help. I deleted the Arial Font Macro and pasted the function into a module. Although it does not show up on the macro list - I called the function name - and it worked! Thank you to Graham for the Batch Auto Addin. This will be a life saver - especially when you have really deeply nested folders and a million files to process. Doing it manually has given me RSI - its a nightmare to process that many documents manually. Thank you to the kind help of the PRO VBA programmers! Kudos! Now I have to try and convert all my macros to functions Thanks again J |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Run a macro on multiple docx. files | Peter Carter | Word VBA | 27 | 12-15-2022 04:10 PM |
Help - Need Macro to Apply Blur | SSL | PowerPoint | 6 | 07-26-2015 08:59 AM |
.wda file extension added after .pdf, .docx, .xls, etc. | GaryofLG | Outlook | 1 | 06-07-2015 11:13 PM |
Macro to apply style to selected tables | ubns | Word | 1 | 08-02-2012 04:09 AM |
Macro to loop in subfolders, change links, export xml data | Catalin.B | Excel Programming | 2 | 09-08-2011 11:37 PM |