Group:  Microsoft Word ยป microsoft.public.word.vba.beginners
Thread: InsertAfter problems

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

InsertAfter problems
"Lilivati" <Lilivati[ at ]gmail.com> 29.06.2006 18:58:58
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.

Re: InsertAfter problems
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 29.06.2006 19:29:26
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

"Lilivati" <Lilivati[ at ]gmail.com> wrote in message
news:1151607538.122641.302130[ at ]x69g2000cwx.googlegroups.com...
[Quoted Text]
>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.
>


Re: InsertAfter problems
"Dave Lett" <davelett[ at ]NOaolSPAM.com> 29.06.2006 19:34:13
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


"Lilivati" <Lilivati[ at ]gmail.com> wrote in message
news:1151607538.122641.302130[ at ]x69g2000cwx.googlegroups.com...
[Quoted Text]
>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.
>


Re: InsertAfter problems
Helmut Weber <nbhymsjxdgcn[ at ]mailinator.com> 30.06.2006 00:48:48
Hi Lilivati,

[Quoted Text]
>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.




Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net