MS Office Forum / Word / Programming / October 2006
Find page location (number) of specific words in a document
|
|
Thread rating:  |
Excel 009 - 08 Oct 2006 05:30 GMT I am new to Word VBA, although I have been programming in Excel VBA.
I need to create an Index page of a document and I guess the way to do this is to specify the words that I to index and have the VBA code run a loop on the words find in the document and returns their page number. Would someone help me to do this? Of course, there might be a better way of doing this. I am open to suggestion.
- 009
Shauna Kelly - 08 Oct 2006 06:45 GMT Hi
To avoid reinventing the wheel, why not use the existing Index functionality?
There are two ways to proceed. One is to mark within the text each of the words that you want to put in the index. The second is to create what's known as a concordance file, that lists all the words that you want to appear in the index, wherever they appear.
The first is a good solution when you want to create an index that is useful for readers. The second is a good solution when someone said "add an index to this document", but no-one really cares whether it's useful<g>.
In any case, look up Index in Word's (not Word VBA's) help file.
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
>I am new to Word VBA, although I have been programming in Excel VBA. > [quoted text clipped - 5 lines] > > - 009 Excel 009 - 08 Oct 2006 13:58 GMT Thank you Shauna.
I will like to try both the Non-VBA and VBA approaches. Reason for the VBA approach is for the learning experience. Moreover, program with VBA will give me more flexiblity to conduct the index page in the format that I needed.
Could you help? Thanks again in advance.
Helmut Weber - 08 Oct 2006 19:17 GMT Hi,
>VBA approach is for the learning experience. if so, like this:
Sub Test8001() Dim rDcm As Range Set rDcm = ActiveDocument.Range With rDcm.Find .Text = "paraphernelia" While .Execute Debug.Print rDcm.Information(wdActiveEndPageNumber) Wend End With End Sub
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Excel 009 - 09 Oct 2006 00:39 GMT Thank you, Helmut. Your code works very well for me. I attached my revised code to return somthing to this user group. It should come in handy for others who need it.
Currently, I return the output in the Immediate Window. How can I return the output at the end of the document in a new page and in 3 columns? Also, how can I make the keyword in bold?
- 009
============================================ Sub Test8001() Dim rDcm As Range Dim strWordPage As String Dim strWord As String Dim intWordPage As Integer Dim intLoopCounter As Integer Dim arrWord Dim i As Integer
arrWord = Array("Statistic", "VBA", "Chart", "Office", "Windows", "FrontPage", "DDE", "Update")
For i = 0 To UBound(arrWord)
strWordPage = "" intLoopCounter = 0 intWordPage = 0
Set rDcm = ThisDocument.Range
With rDcm.Find .Text = arrWord(i) While .Execute intLoopCounter = intLoopCounter + 1
'Record only when the current page number is different If rDcm.Information(wdActiveEndPageNumber) <> intWordPage Then If intLoopCounter = 1 Then strWordPage = strWordPage & " " & rDcm.Information(wdActiveEndPageNumber) Else strWordPage = strWordPage & ", " & rDcm.Information(wdActiveEndPageNumber) End If Else 'Do Nothing End If
intWordPage = rDcm.Information(wdActiveEndPageNumber) Wend End With
Debug.Print arrWord(i) & " " & strWordPage Set rDcm = Nothing Next i
End Sub ============================================
Russ - 09 Oct 2006 01:32 GMT One way I learn about code is to record a macro while I manually do what I want to do with the mouse selecting text and manipulating menus and dialogs, etc. Then I stop recording and inspect the code that the recorder recorded. This method doesn't always show you the best way or efficient way to do something but it is a way to start getting answers. Also if you press the F1 function key while your cursor is in a word in VBA code, then the VBA Help window pops up to inform you.
> Thank you, Helmut. Your code works very well for me. I attached my > revised code to return somthing to this user group. It should come in [quoted text clipped - 56 lines] > End Sub > ============================================
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Excel 009 - 09 Oct 2006 02:12 GMT Thanks Russ. I did what you suggested and record the code for the following tasks:
- Insert a page break at the end of the document. - Set a 3-column format from that page forward.
I now need to:
- Go to the last page. - Populate the output using my code to the last page of the document. - Select each of the keywords and make them bold.
I also need to know how I can run my code on a specific range of the document, example, page 5 to 7, instead of on the whole document?
Could any one help?
Russ - 09 Oct 2006 04:21 GMT excel009, The cod below might get you started.
> Thanks Russ. I did what you suggested and record the code for the > following tasks: [quoted text clipped - 7 lines] > - Populate the output using my code to the last page of the document. > - Select each of the keywords and make them bold. Sub aTest() Dim rngRange As Range Dim strText As String
strText = "Test and more words" Set rngRange = ActiveDocument.Range rngRange.Start = rngRange.End rngRange.InsertAfter strText With rngRange.Find .Text = "test" ' you may want use wildcards .Replacement.Text = "^&" .Replacement.Font.Bold = True .Execute Replace:=wdReplaceAll End With End Sub
> I also need to know how I can run my code on a specific range of the > document, example, page 5 to 7, instead of on the whole document? Pages are defined in Word by the default printer driver. But you could research using the 'goto' method in Word VBA help.
> Could any one help?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 09 Oct 2006 04:39 GMT excel009,
> The code below might get you started. > [quoted text clipped - 29 lines] > Pages are defined in Word by the default printer driver. But you could > research using the 'goto' method in Word VBA help. Also if you could uniquely identify the start and end of the part of the document you want to work on, you could insert continuous section breaks before and after or create a bookmark and address that section.range or bookmark.range with your code. You can also address the tables collection, if those pages contain tables that you want to work on.(since you are coming from Excel?)
>> Could any one help?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Excel 009 - 09 Oct 2006 17:09 GMT Thank Russ.
I have tried to incorporate the code into my current code, but unsuccessful to achieve my objective. Your code works fine - it is just me.
I think may be it would be easier to separate the task into 2:
- Populate my output at the end of last page. - Make the selected keywords bold.
Assume the following is my output already populated at the end of the last page of the document:
VBA 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12 Chart 6, 7, 9, 11, 12 Office 2, 7, 8, 10, 12 Windows 2, 3, 4, 8, 9, 10, 12 FrontPage 7, 8 DDE 5, 10, 11 Update 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
How can I make the keywords, "VBA", Chart"....."Update", bold, and not the page numbers that append to them?
Russ - 09 Oct 2006 18:04 GMT One quick reply is to change find and replace to a wildcard find. Amend code I suggested with this.
.text = "[A-Za-z]{1,}" 'look for groups of letters .matchwildcards = True
> Thank Russ. > [quoted text clipped - 20 lines] > How can I make the keywords, "VBA", Chart"....."Update", bold, and not > the page numbers that append to them?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Excel 009 - 09 Oct 2006 18:52 GMT Would this search for all the keywords in the document or just my output at the end of the page? How can I use the code to select the keywords and make them bold?
Russ - 10 Oct 2006 05:18 GMT Amend my code as suggested and run it. You can run it is single step mode to see what each line does. You did say you were re-inventing Word's index function to learn about VBA?
> Would this search for all the keywords in the document or just my > output at the end of the page? End of document with my amended code.
> How can I use the code to select the > keywords and make them bold? My amended macro inserts predefined text at end of document. (Define your own Index text) Then the amended wildcard pattern find and replace finds all groups that have strings of letters in that predefined text at the end of the document and makes them bold. Click on this link for wildcard pattern info. http://www.gmayor.com/replace_using_wildcards.htm
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Excel 009 - 10 Oct 2006 17:58 GMT Thanks Russ.
I incorporated your code into my little sub and it worked in the way that I needed this time. I am posting the code for others as a reference.
I used your code with an array for the keywords:
.Text = arrWord(i)
so that all the keywords will be bolded.
Thanks again for all your help. This has been a good learning experience. I am now going to explore the Index function in Word without using the VBA. I had problem using it last time and that led me to use the VBA approach. I am going to give it another try. Should I fail this time, you should see my post again.
Excel 009
-------------------------------------------------------------------------------------
Sub Test8001() Dim rDcm As Range Dim strWordPage As String Dim strWord As String Dim intWordPage As Integer Dim intLoopCounter As Integer Dim arrWord Dim i As Integer Dim strIndex As String
arrWord = Array("Statistic", "VBA", "Chart", "Office", "Windows", "FrontPage", "DDE", "Update")
For i = 0 To UBound(arrWord) strWordPage = "" intLoopCounter = 0 intWordPage = 0
Set rDcm = ThisDocument.Range
With rDcm.Find .Text = arrWord(i) While .Execute intLoopCounter = intLoopCounter + 1
'Record only when the current page number is different If rDcm.Information(wdActiveEndPageNumber) <> intWordPage Then If intLoopCounter = 1 Then strWordPage = strWordPage & " " & rDcm.Information(wdActiveEndPageNumber) Else strWordPage = strWordPage & ", " & rDcm.Information(wdActiveEndPageNumber) End If Else 'Do Nothing End If
intWordPage = rDcm.Information(wdActiveEndPageNumber) Wend End With
strIndex = strIndex & arrWord(i) & " " & strWordPage & vbCr 'Debug.Print arrWord(i) & " " & strWordPage
Set rDcm = Nothing Next i
Dim rngRange As Range Set rngRange = ThisDocument.Range rngRange.Start = rngRange.End rngRange.InsertAfter vbCrLf & strIndex
For i = 0 To UBound(arrWord) With rngRange.Find .Text = arrWord(i) .Replacement.Text = "^&" .Replacement.Font.Bold = True .Execute Replace:=wdReplaceAll End With Next i End Sub
|
|
|