Index Document Macro

N

NYSA-HD

Does anyone know how to write a vba macro in Word that would place an index
in the active document, but prompt the user for directory, and look at all
files in that directory and read all of those files for the index markers to
create the index in the active document referring to pages and markers in the
files in the directory chosen by the prompt? Basically create an index
document where all the markers span separate files in a separate directory
and all page numbers and indexed items are driven from those separate files.
 
D

Dave Lett

Hi,

You can use fields:

{ INDEX }
{ RD C:\\Manual\\Chapters\\Chapter1.doc }
{ RD C:\\Manual\\Chapters\\Chapter2.doc }
{ RD C:\\Manual\\Chapters\\Chapter3.doc }

HTH,
Dave
 
N

NYSA-HD

I am aware of how you can use fields to reference other docs for indexing,
but w/o manually creating a few hundred fields, how can I have a macro read
the directory and create the index fields referencing all files in a folder
for me?
 
D

Dave Lett

Hi again,

Have a look at the article "How to read the filenames of all the files in a
directory into an array" at http://word.mvps.org/FAQs/General/index.htm

You'll probably end up with something like the following:

Dim MyFile As String
Dim Counter As Long
Dim sFolder As String
'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)

With Dialogs(wdDialogCopyFile)
.Display
sFolder = Replace(.Directory, Chr(34), "")
End With

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(sFolder & "*.doc")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
Counter = Counter + 1
Loop

'Reset the size of the array without losing its values by using Redim
Preserve
ReDim Preserve DirectoryListArray(Counter - 1)

For Counter = 0 To UBound(DirectoryListArray)
Set oRng = ActiveDocument.Range(0, 0)
ActiveDocument.Fields.Add Range:=oRng, _
Type:=wdFieldRefDoc, _
Text:=Replace(sFolder, "\", "\\") & DirectoryListArray(Counter), _
Preserveformatting:=False
oRng.InsertParagraphAfter
Next Counter

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top