G
Gabe Knuth
Hi All,
I have a Word document for which I'm trying to create an index. I've
just started this code, but the idea is to parse through each word and
dump out the word and the page number to a CSV file that I can
manipulate later on (or dump it to a dictionary object or array or
something so I can manipulated it in code). The end result being the
100 least used words in the document should make a halfway decent
index.
My problem is that I can't figure out how to get the the page number
to show up correctly using the following code:
Function CreateIndex()
Dim fso, NewsFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set File = fso.CreateTextFile("c:\book\sampleindex.csv", True)
Set objDictionary = CreateObject("Scripting.Dictionary")
Set colWords = Application.ActiveDocument.Words
For Each strWord In colWords
strWord = LCase(strWord)
strLetter = Left(strWord, 1)
If Asc(strLetter) < 97 Or Asc(strLetter) > 122 Then
Else
ActiveWindow.ScrollIntoView Selection.Range, True
intPage = Selection.Range.Information(wdActiveEndPageNumber)
MsgBox (strWord & "," & intPage)
File.WriteLine strWord & "," & intPage
End If
Next
File.Close
End Function
I'm pretty new to VBA for Word, but I'm not terrible at VBS, so I can
sometimes keep up with you über scripters out there.
The result of this script is a CSV file that has each word in the
document, and the number "1", because wdActiveEndPageNumber always
returns a "1". I tried to add the "ActiveWindow.ScrollIntoView" line
to scroll to the page, but I have a feeling I'm missing something for
that to work.
So, any help you guys can offer would be much appreciated.
Thanks!
Gabe
I have a Word document for which I'm trying to create an index. I've
just started this code, but the idea is to parse through each word and
dump out the word and the page number to a CSV file that I can
manipulate later on (or dump it to a dictionary object or array or
something so I can manipulated it in code). The end result being the
100 least used words in the document should make a halfway decent
index.
My problem is that I can't figure out how to get the the page number
to show up correctly using the following code:
Function CreateIndex()
Dim fso, NewsFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set File = fso.CreateTextFile("c:\book\sampleindex.csv", True)
Set objDictionary = CreateObject("Scripting.Dictionary")
Set colWords = Application.ActiveDocument.Words
For Each strWord In colWords
strWord = LCase(strWord)
strLetter = Left(strWord, 1)
If Asc(strLetter) < 97 Or Asc(strLetter) > 122 Then
Else
ActiveWindow.ScrollIntoView Selection.Range, True
intPage = Selection.Range.Information(wdActiveEndPageNumber)
MsgBox (strWord & "," & intPage)
File.WriteLine strWord & "," & intPage
End If
Next
File.Close
End Function
I'm pretty new to VBA for Word, but I'm not terrible at VBS, so I can
sometimes keep up with you über scripters out there.
The result of this script is a CSV file that has each word in the
document, and the number "1", because wdActiveEndPageNumber always
returns a "1". I tried to add the "ActiveWindow.ScrollIntoView" line
to scroll to the page, but I have a feeling I'm missing something for
that to work.
So, any help you guys can offer would be much appreciated.
Thanks!
Gabe