find text and then insert TOA at the end of text

W

Winston

Hello Everyone,

I have a document which I would like to search for particular text
beginning and ending with special characters and then insert at the
end of the text a Table of Authorities field code with the text found.

So far the following code finds the text but the field code is
inserted at the curser position where I start the macro and not after
the point where it finds the text.

eg. Text in Word document:

..yy.Text to go into table of authorities.e.

I would like the macro to do this:

..yy.Text to go into table of authorities.e.{ TA \l "Text to go into
table of authorities" \c 1 }

Any help in pointing out what is wrong with this code would be greatly
appreciated!,

Winston


Sub Macro1main()
'
' Macro1main Macro
'
'
Dim oRng3 As Word.Range
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[.][y][y][.]*[.][e][.]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True

While .Execute

oRng.Start = oRng.Start + 4
oRng.End = oRng.End - 3

insertTOAmacro oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub insertTOAmacro(ByVal oRng2 As Range)
ActiveDocument.TablesOfAuthorities.MarkCitation _
Range:=Selection.Range, ShortCitation:=Selection.Range.Text, _
LongCitation:=oRng2.Text, Category:=1
End Sub
 
D

Doug Robbins - Word MVP

I think that this will do what you want:

Dim oRng As Range, oRng2 As Range, oRng3 As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[.][y][y][.]*[.][e][.]",
MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
Set oRng = Selection.Range
Set oRng2 = Selection.Range.Duplicate
Set oRng3 = Selection.Range.Duplicate
oRng2.Start = oRng2.Start + 4
oRng2.End = oRng2.End - 3
oRng.Collapse wdCollapseEnd
ActiveDocument.TablesOfAuthorities.MarkCitation _
Range:=oRng, ShortCitation:=oRng2.Text, _
LongCitation:=oRng3.Text, Category:=1
Loop
End With

though the Citations may not be what you wanted.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

Wiston,
Hello Everyone,

I have a document which I would like to search for particular text
beginning and ending with special characters and then insert at the
end of the text a Table of Authorities field code with the text found.

So far the following code finds the text but the field code is
inserted at the curser position where I start the macro and not after
the point where it finds the text.

eg. Text in Word document:

.yy.Text to go into table of authorities.e.


I would like the macro to do this:

.yy.Text to go into table of authorities.e.{ TA \l "Text to go into

table of authorities" \c 1 }

Any help in pointing out what is wrong with this code would be greatly
appreciated!,

Winston


Sub Macro1main()
'
' Macro1main Macro
'
'
Dim oRng3 As Word.Range
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[.][y][y][.]*[.][e][.]"

Why use all the square brackets?
..Text = ".yy.*.e."
Should be sufficient.
See how to use those brackets at:
http://www.gmayor.com/replace_using_wildcards.htm
And use:
..MatchCase = True
,If you're worried about matching lower and upper cases.
 

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