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
|