How do I replace text with a field?

W

Wibs

Hi all,

I want to index a book I have finished that contains hundreds of
personal names and places. Fortunately they all begin with capital
letters. I now know how to search for all words in a document that
begin with a capital letter <[A-Z] but replacing the found text with
the index mark field and including the found text within the quotes I
believe to be outside of the capabilities of the Search and Replace
functionality.

Can anyone suggest how this might be achieved in VBA?
 
G

Greg Maxey

Wibs,

The following should index every Capitalized word:
Sub IndexAllWords()
Dim oFld As Field
Dim oWord As Range
Dim oDoc As Document

Set oDoc = ActiveDocument
With ActiveWindow.View
.ShowFieldCodes = False
.ShowHiddenText = False
.ShowAll = False
End With
'Prevent double indexing
For Each oFld In oDoc.Fields
If oFld.Type = wdFieldIndexEntry Then oFld.Delete
Next

For Each oWord In oDoc.Words
If oWord Like "[A-z]*" Then
If Not oWord.Characters.Last Like "[A-z]" Then
oWord.End = oWord.End - 1
End If
oDoc.Indexes.MarkEntry Range:=oWord, Entry:=Trim(oWord.Text)
End If
Next oWord

End Sub
 
W

Wibs

Greg said:
Wibs,

The following should index every Capitalized word:
Sub IndexAllWords()
Dim oFld As Field
Dim oWord As Range
Dim oDoc As Document

Set oDoc = ActiveDocument
With ActiveWindow.View
.ShowFieldCodes = False
.ShowHiddenText = False
.ShowAll = False
End With
'Prevent double indexing
For Each oFld In oDoc.Fields
If oFld.Type = wdFieldIndexEntry Then oFld.Delete
Next

For Each oWord In oDoc.Words
If oWord Like "[A-z]*" Then
If Not oWord.Characters.Last Like "[A-z]" Then
oWord.End = oWord.End - 1
End If
oDoc.Indexes.MarkEntry Range:=oWord, Entry:=Trim(oWord.Text)
End If
Next oWord

End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Many thanks for that Greg. I pasted the code in, and when I ran the
macro the Index produced was to every word in the document. I tweaked
the macro from [A-z] to [A-Z] and this time it indexed all the Proper
Nouns, but with the last character trimmed off. So I tweakied the macro
again removing the -1, and now it works a treat.

But alas, I fear the Gods are against me. There are just too many
compound places and names, such as Kirby Wiske, Barrow-in-Furness, Sir
Peter Henry Giles jun., to complicate matters. I was hoping I could
just easily delete all the words beginning with a capital that start
every sentence.

Looks like there are no real shortcuts, I will just have to go through
the entire 300+ pages and index every proper noun by hand
(Alt+Shift+X), and type in the correct way round for people's names,
i.e (John Smith) to (Smith, John) and use the /f switch to separate the
entries into two indexes, an Index Nominum and and Index Locorum.

Long sighhhhhhhhhhhhh. I wonder how many cups of tea this will take?

Thanks again,

Wibs
 
G

Greg Maxey

Wibs,

The macro I have on file does index every word by intent. You were right
in changing the first A-z to A-Z. The second range should be A-z as it is
intended to just clip spaces and punctuation.

I see your problem wrt to proper names and such.




--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Greg said:
Wibs,

The following should index every Capitalized word:
Sub IndexAllWords()
Dim oFld As Field
Dim oWord As Range
Dim oDoc As Document

Set oDoc = ActiveDocument
With ActiveWindow.View
.ShowFieldCodes = False
.ShowHiddenText = False
.ShowAll = False
End With
'Prevent double indexing
For Each oFld In oDoc.Fields
If oFld.Type = wdFieldIndexEntry Then oFld.Delete
Next

For Each oWord In oDoc.Words
If oWord Like "[A-z]*" Then
If Not oWord.Characters.Last Like "[A-z]" Then
oWord.End = oWord.End - 1
End If
oDoc.Indexes.MarkEntry Range:=oWord, Entry:=Trim(oWord.Text)
End If
Next oWord

End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Many thanks for that Greg. I pasted the code in, and when I ran the
macro the Index produced was to every word in the document. I tweaked
the macro from [A-z] to [A-Z] and this time it indexed all the Proper
Nouns, but with the last character trimmed off. So I tweakied the
macro again removing the -1, and now it works a treat.

But alas, I fear the Gods are against me. There are just too many
compound places and names, such as Kirby Wiske, Barrow-in-Furness, Sir
Peter Henry Giles jun., to complicate matters. I was hoping I could
just easily delete all the words beginning with a capital that start
every sentence.

Looks like there are no real shortcuts, I will just have to go through
the entire 300+ pages and index every proper noun by hand
(Alt+Shift+X), and type in the correct way round for people's names,
i.e (John Smith) to (Smith, John) and use the /f switch to separate
the entries into two indexes, an Index Nominum and and Index Locorum.

Long sighhhhhhhhhhhhh. I wonder how many cups of tea this will take?

Thanks again,

Wibs
 

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