TechWhirl (TECHWR-L) is a resource for technical writing and technical communications professionals of all experience levels and in all industries to share their experiences and acquire information.
For two decades, technical communicators have turned to TechWhirl to ask and answer questions about the always-changing world of technical communications, such as tools, skills, career paths, methodologies, and emerging industries. The TechWhirl Archives and magazine, created for, by and about technical writers, offer a wealth of knowledge to everyone with an interest in any aspect of technical communications.
That Visual Basic macro for multiple files (a reprint...)
Subject:That Visual Basic macro for multiple files (a reprint...) From:JIMCHEVAL -at- AOL -dot- COM Date:Wed, 15 Jul 1998 19:24:32 EDT
A week or two back, someone asked about a Visual Basic macro that processes
multiple files. I thought they might be referring to an old post of mine, but
for the life of me couldn't find the code they were requesting (I think I was
at a client site when I did.)
Then it struck me - it's in the ARCHIVE...
from which the following reprint:
==========================================================
Here's some more sample code for Word 97 macros. It's also an invitation to
help, since the Word 97 version isn't as tight as I'd like it (see comments
below.)
The core of this macro is the fact that it accesses each file in a selected
directory. There are numerous uses for this basic function - any time you've
written a simple macro and wished you could apply it to every file in the
directory, this is basically what you needed. Some people may just be happy
to have a macro that lists every file in a directory (which is what this
does.)
First, the Word 6 (originally Word 2) version:
===================================================
Sub MAIN
'This macro uses the File Find dialogue to set criteria for
' a list of files (directory, extension, etc.).
'It then inserts each file name in the current document
'(Word 6 version - converted from Word 2)
' Jim Chevallier - jimcheval -at- aol -dot- com
'
WW2_FileFind .View = 3
' Assign the Word File Find dialogue box to 'dlg'
Dim dlg As WW2_FileFind
' Get the current values for dlg (=the Word File Find box)
GetCurValues dlg
' Assign the number of found files to variable 'c'
c = CountFoundFiles()
'Using a 'For' loop, run through all the files
For x = 1 To c
'Insert each file name into the current document
' (files are referenced as elements in the array FoundFileName$)
A$ = FoundFileName$(x)
WW2_Insert A$
'Insert a paragraph acter each file name (otherwise they all line up on one
line)
InsertPara
Next
End Sub
=============================================================
This is the Word 97 (Visual Basic) version. There's a few key differences.
The big one is that "FileSearch" is now treated as a property of the
"Application" object. This has the advantage that the technique no longer
applies just to Word - any Office application's file search tool can use the
same instruction.
What is not so obvious is how Application.FileSearch interacts with Word's
own File Search box. So far as I can tell, it does NOT simply pick up the
parameters set when you go into File Open (which has been combined with
FileFind in Word 97). So I have grabbed some of the parameters from Word's
dialogue box and assigned them to the corresponding parameters for
Application.Search. But I suspect there's a more elegant way to do this.
=============================================================
Sub InsertFileNames()
'
' InsertFileNames Macro
' Macro created 08/07/97 by Jim Chevallier
'
'Assign the values of Word's File Find (Search) dialogue box to 'mydialog'
Set mydialog = Dialogs(wdDialogFileFind)
'Assign certain info from the dialogue box to variables
'(This will pick up whatever you set when you went into File Open)
S$ = mydialog.SearchPath
N$ = mydialog.Name
'Assign the more generic Application.FileSearch to the 'Fs' variable
Set Fs = Application.FileSearch
'Use the With instruction to work with Fs as the implied high-level qualifier
' (i.e., Fs.Lookin, Fs.MatchTestExactly, etc.)
With Fs
'Assign the values captured from WdFileFind to the Application.FileSearch
variables
.LookIn = S$
.FileName = N$
'Set other parameters for Application.FileSearch
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
.SearchSubFolders = 1
'Test if the execution was successful
If .Execute() > 0 Then
'Using a 'For' loop, go through the found files
For i = 1 To .FoundFiles.Count
'Insert each file name in the current document
Selection.InsertAfter Text:=.FoundFiles(i)
'Insert a paragraph after each file name
Selection.InsertParagraphAfter
Next I
'If execution was not successful, display a message
Else
MsgBox "There were no files found."
End If
End With
End Sub
======================================================
Note that with both versions, it is possible to insert any number of other
instructions in the For loop and so apply them to every one of the selected
documents (which of course means you have to first open and last close each
of the found documents.)