InsertAfter problems

L

Lilivati

I am playing around with VBA in Word to become familiar with it before
beginning a project. So far it has been going well, but I am currently
stumped on this InsertAfter issue. Basically to my mind it should
insert text after the selection, but on the same line. What it is
doing is inserting the text on a new line, which Word then considers to
be a new paragraph. Here is my little practice code:

Sub Test()
Options.ReplaceSelection = False

Dim i As Integer

For i = 1 To 3
Paragraphs(i).Range.Select
Selection.Copy
Application.Selection.InsertAfter " -i value: " & i & "- "
Selection.Paste
Next i

' (this is in here because leaving it false does some annoying things
to how backspace works)
Options.ReplaceSelection = True
End Sub

I am sure there is something simple here I am missing, but for the life
of me I can't figure out what.
 
D

Doug Robbins - Word MVP

If you want to add " - i value: #- " where # is the value of i to the end of
the paragraph, then use

Dim i As Long
Dim para As Range
For i = 1 To 3
Set para = ActiveDocument.Paragraphs(i).Range
para.End = para.End - 1
para.InsertAfter " -i value: " & i & "- "
Next i


--
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
 
D

Dave Lett

Hi,
You can get avoid Selection by using ranges as in the following, which does
what I think you're looking for:

Dim i As Integer
Dim oRng As Range
For i = 1 To 3
Set oRng = ActiveDocument.Paragraphs(i).Range
oRng.MoveEnd unit:=wdCharacter, Count:=-1
oRng.Copy
oRng.InsertAfter " -i value: " & i & "- "
Next i

In the original code, the new text is being inserted at the beginning of the
next paragraph (not at the end like you want) because that's what the code
is TELLING it to do. That is, the line
Paragraphs(i).Range.Select
includes the paragraph mark of the "i" paragraph. Therefore, when you insert
something after it, that something will appear at the beginning of the next
paragraph.

HTH,
Dave
 
H

Helmut Weber

Hi Lilivati,
I am playing around with VBA in Word to become familiar with it

if so, you might enjoy this sample, too:

Dim i As Long
For i = 1 To 3
With ActiveDocument.Paragraphs(i)
.Range.Characters.Last.Previous.InsertAfter _
" -i value: " & i & "- "
End With
Next

Sometimes I find things like that very handy:
....characters.last
....characters.last.next
....characters.last.previous
....characters.first.next
....characters.first.previous
....

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

But if you apply this to the last paragraph in the doc,
you'll get a new empty paragraph at the doc's end.
 

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