Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2015, 11:14 AM
jc491's Avatar
jc491 jc491 is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2013
VBA Novice
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 09-10-2015, 09:52 PM
gmayor's Avatar
gmayor gmayor is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 09-11-2015, 05:01 AM
jc491's Avatar
jc491 jc491 is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2013
VBA Novice
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

Hi Graham,
thank you for your help let me test this.

J
Reply With Quote
  #4  
Old 09-11-2015, 05:09 AM
jc491's Avatar
jc491 jc491 is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2013
VBA Novice
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 09-11-2015, 06:18 AM
gmaxey gmaxey is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 32bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 09-11-2015, 06:40 AM
jc491's Avatar
jc491 jc491 is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2013
VBA Novice
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 09-11-2015, 07:01 AM
gmaxey gmaxey is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 32bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 09-11-2015, 07:26 AM
jc491's Avatar
jc491 jc491 is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2013
VBA Novice
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

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
Reply With Quote
  #9  
Old 09-11-2015, 08:31 AM
jc491's Avatar
jc491 jc491 is offline How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Windows 7 64bit How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Office 2013
VBA Novice
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension 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
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Macro to apply style to selected tables ubns Word 1 08-02-2012 04:09 AM
How To Apply A VBA Macro to All Subfolders in a Directory of a docx. Extension Macro to loop in subfolders, change links, export xml data Catalin.B Excel Programming 2 09-08-2011 11:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:58 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft