Google Answers Logo
View Question
 
Q: Word Macro expert help ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Word Macro expert help
Category: Miscellaneous
Asked by: rohittyagi-ga
List Price: $3.00
Posted: 21 Sep 2005 15:07 PDT
Expires: 21 Oct 2005 15:07 PDT
Question ID: 570676
I know how to set up Macros in MS Word. I want to run a macro on a
number of files, so what I am doing is manually opening each file runt
the macro and doing the same thing over and over again. It saves me a
lot of time, but I still desire more and am certain that there is a
way. So my question is How do I automatically run this macro for all
the files contained within a Folder, without having to intervene. In
other words just one command, goes in and applies the macro to all the
files within a given directory.
Answer  
Subject: Re: Word Macro expert help
Answered By: maniac-ga on 21 Sep 2005 18:55 PDT
Rated:5 out of 5 stars
 
Hello Rohittyagi,

I am going to assume you want to run MS Word on Windows. If using Word
on a Macintosh, please make a clarification request and I can give you
similar guidance for that platform as well.

The code you want will look something like the following:

    Dim fsSearch As FileSearch
    Set fsSearch = Application.FileSearch
    With fsSearch
        .FileName = "*.*"
        .LookIn = "C:\My Documents"
        .Execute
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i 
    End With

To explain the code above,
 - the Dim / Set statements create / initialize a "FileSearch" object
 - the statements .Filename and .LookIn set up the file type / directory to search
 - the .Execute statement fills in the FileSearch object with all the
matching files
 - the for loop goes through each file and displays the name in a message box

In your case, replace the line
  MsgBox .FoundFiles(i)
with the appropriate macro call or put the code in line.

You may also want to replace the string constant "C:\My Documents"
with the directory of your choice, or preferrably select a directory
using an InputBox (prompt the user for the path) or one of Word's
built in dialog box.

For more information, search the Visual Basic online help using phrases like
  FileSearch
  dialog box
  prompting for information
or similar phrases. You can also search on the web using those phrases
and add Microsoft Word - for example
  Microsoft Word FileSearch

I also strongly suggest using the Object Browser in Visual Basic. Use
  View -> Object Browser
to display the browser and enter a phrase in the top of the window. At
the bottom are the matching objects / methods - select one and then
the help (?) button to go directly to that part of the on line help.

Please make a clarification request if some part of the answer is
unclear or incomplete.

Good luck with your work.

  --Maniac

Request for Answer Clarification by rohittyagi-ga on 22 Sep 2005 08:41 PDT
The Macro I want to run is Normal.NewMacros.FontSize


So should the code read = 

Sub Macro1()
    Dim fsSearch As FileSearch
    Set fsSearch = Application.FileSearch
    With fsSearch
        .FileName = "*.doc"
        .LookIn = "C:\Macro Test"
        .Execute
        For i = 1 To .FoundFiles.Count
            Normal.NewMacros.FontSize (i)
                       
        Next i
    End With

Request for Answer Clarification by rohittyagi-ga on 22 Sep 2005 08:48 PDT
Actually let me clarify my previous question - 

    Dim fsSearch As FileSearch
    Set fsSearch = Application.FileSearch
    With fsSearch
        .FileName = "*.*"
        .LookIn = "C:\My Documents"
        .Execute
        For i = 1 To .FoundFiles.Count
After this I want to first open the file at ith position and make a
call to my macro against that open file. Please advise. Thanks

Rohit

Request for Answer Clarification by rohittyagi-ga on 22 Sep 2005 10:56 PDT
I got it. Thanks a lot. Your answer was very helpful and
understandable. The object viewer tip was great. I learned much more
today.

Clarification of Answer by maniac-ga on 22 Sep 2005 18:27 PDT
Hello Rhittyagi,

It appears you already figured out the open / save / close problem but
let me outline the method I use to solve that kind of problem (in case
others are following along). Using the menu selection
  Tools -> Macro -> Record New Macro
and using an unused (or the default) macro name, I do the following steps:
 [1] open a file using the File -> Open
 [2] run the macro
 [3] File -> Save (or Save As...)
 [4] File -> Close
 [5] stop recording

Then go back into Visual Basic and look at the recorded steps. This
gives you the basic commands you need to add to open the file, run the
macro, save the changes, and close the file. The modifications are
pretty straight forward to include inside the loop. Basically replace
the fixed file names with the values returned from FoundFiles(i).

I also suggest a check of the on line help for the specific commands
(e.g., Documents.Open, ActiveDocument.Save). For example, using
AddToRecentFiles:=False will prevent the files opened by the macro
from appearing in the "recent files" list.

  --Maniac
rohittyagi-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy