1
votes

VBA macro: replace text in word file in all sub folders

I'm very new to VBA. What i want to be able to have is a macro attached to a button in excel that can 'on click':

  1. allow me to navigate to a folder
  2. Search that folder for any word files (.doc)
  3. Then perform a find and replace for a specific string of text that may be within any of those .doc files with the folder.
  4. close and save the amended .doc's

I found the question (please see link) which seems very similar code to what i need, although i dont need to go through any sub folders, just want to search through one folder that i navigate to at the start of the process.

I dont know enough about VBA to edit code confidently and would really appreciate your help with a solution.

Thank you,

Alex

1

1 Answers

0
votes

We'd be glad to help with a solution. But you need to demonstrate a minimal understanding of the problem being solved, see the SO question checklist for details. Please note that your question does not meet Stack Overflow's guidelines as it stands right now, but it's easy to fix! After you review the guidelines, click "edit" under your question and polish it up a bit.

Everyone starts out being new to something. Being new, you have a few good options for getting your project started.

You may try using the code in the link you've pasted above to start, and try modifying it somewhat for your own purposes.

One of the best options is to record a macro, then go into the VBA editor (by pressing Alt-F11) to look at the code the macro made. This lets you see how code is set up and gives you a starting point for writing your own code.

Also, you can look through the Microsoft help files, available through the help menu in VBA editor. There are numerous Excel VBA websites online as well.


Edit: To use the code in the link you've posted, you can ignore the section that searches for subfolders and adds them to the collection. At the beginning of the next section that loops through the files, there is a statement:

For Each varItem In colSubFolders

Take out this For...Each loop that searches through the subfolders current directory, and just use the code inside it that looks through the Word docs in the subfolder (now looking through Word docs in one folder only, not every subfolder.)