Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word macro - insert autotext at end of each line for highlighted text

Status
Not open for further replies.

dcorleto

MIS
May 19, 2002
82
US
First, thanx for all the help I have received so far in this excellent group...

I would like to highlight/select some lines of text - there is a paragraph mark at the end of each line. I have some autotext already created.

I want to automate going to the end of each line in the highlighted selection, and insert the autotext right before the paragraph mark.

Is this possible?

Please just give me some sort of starting point so that I can hopefully learn something along the way. I can write a basic macro so far to do a go to the end of a line, and then paste in autotext, but that's about all I am capable of so far...

Thanks in advance.
 
Here's the only code I have so far...

Sub insertautotext()
'
' insertautotext Macro
' Macro recorded 11/8/2004 by Dan
'
Selection.EndKey Unit:=wdLine
NormalTemplate.AutoTextEntries("boxwithdash").Insert Where:=Selection. _
Range, RichText:=True
Selection.MoveDown Unit:=wdLine, Count:=1
End Sub
 
Going further -

I have this code now -


Sub autotextinsert()
'
' autotextinsert Macro
' Macro recorded 11/8/2004 by Dan
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13"
.Replacement.Text = "^c^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


It unfortunately does the replace on the entire document, rather than just on what I have selected. However, when I do it manually, the Find and Replace box asks me if I want to continue and I can say no. How can I say the same thing in VBA?
 
Phaed -

Thanks for responding - I have been working on this alot, and learning along the way. Here is what I have come up with so far, rudimentary as it is - I wrote two macros and have one call the other so that it moves from table to table but waits for me to decide if I want to run the code or not. I set the document up to browse by table. I have the text to be inserted already in autotext, so I just insert that somewhere and copy it to the clipboard first, then select a table and run the macro called "yikes" with a keyboard shortcut.

Here is the code of the two macros that I have written - note that I found through trial and error that if I convert the table to text first, the selection is still held, and a paragraph mark replaces the end of the cell squiggly thing that I have no idea what the ascii character code would be for it, since I can't even highlight that character... anyway, here they are...

Sub gotonexttable()
'
' gotonexttable Macro
' Macro recorded 11/16/2004 by Dan
'
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^13"
.Replacement.Text = "^c^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Tables(1).Select
End Sub




Sub yikes()
'
' yikes Macro
' Macro recorded 11/16/2004 by Dan
'Convert table to text then apply contents of the clipboard from autotext
Selection.Rows.ConvertToText Separator:=wdSeparateByParagraphs, _
NestedTables:=True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13"
.Replacement.Text = "^c^p"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Call gotonexttable
End Sub
 
need better specs

is text highlighted (as in REALLY highlighted with highlighter), or selected?

is text in table, or not? why convert table to text???

is inserted autotext always to be at end of line, or end of paragraph?

post answers to above & sure answer is here.

ps. u should learn how 2 use Range object, not Selection.


 
Code:
Sub InsertMyAuto()
Dim r As Range
Selection.Collapse direction:=wdCollapseEnd
Set r = ActiveDocument.Bookmarks("\para").Range
    r.Collapse direction:=wdCollapseEnd
    r.MoveEnd unit:=wdCharacter, Count:=-1
    Selection.Move unit:=wdCharacter, Count:=r.Start - Selection.End
NormalTemplate.AutoTextEntries("boxwithdash").Insert Where:=Selection. _
        Range, RichText:=True
Set r = Nothing
End Sub

inserts autotext @ end of paragraph with ANY text selected.

collapses selection
makes Range of current paragraph
collapse Range (this include para mark)
move Range back 1 to NOT include para mark
moves selection point to Range end (just before para mark)
insert autotext

close to what u want?
 
is text highlighted (as in REALLY highlighted with highlighter), or selected?

-> Text is selected

is text in table, or not? why convert table to text???

-> Text is in a table (actually all in one cell). When I turn on invisibles, the last character in the table (cell) is not a paragraph mark, therefore If I just have the code find paragraph marks and paste text, the last line of the table does not get processed.

is inserted autotext always to be at end of line, or end of paragraph?
-> Always at the end of a paragraph.

Hope this answers your questions...
 
what is relation of selected text to what u r doing?

"this is text of some kind"

is select "is text of" different from select "this is"? don't understand the use/purpose of selecting any text with insert autotext end of paragraph.

last mark of pargraph in table cell is Chr(7)Chr(13), but anyway use Range, not Selection. use find /replace poor. u r not really want find /replace, u want to go end of paragraph and insert text..yes?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top