InsertAfter function question

V

vbaNOOB

Hi All,

For the range object in VBA, there are insertafter and insertbefore function
that insert text in front or behind the a particular range.

My Question is, when I try to insert text behind the range using insertafter
function.
The inserted text carry the style and formatting from the original range.

Eg. "This is a sentence"
Assume the word "is" is my range and is red in color. when I use insertafter
function (myrange.insertafter(" not")) The new inserted word " not" become
red too.

In this case, I know the inserted word is " not" and is red in color. So I
look for any red " not" and change it back. However, in some cases, I dont
know the orignal style of the text, so I can't change it back.

Is there any way that I can insert text after the range without carrying
range style??
I'm not very sure if it's the problem of "paragraph style" and "character
style".

P.s also, I dont get this problem while i insert text before the range using
"insertbefore" funciton. Why is that??

Many thanks
 
H

Helmut Weber

Hi,

Sub Test601()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "quick"
While .Execute
rDcm.Characters.Last.Next = " not "
Wend
End With
End Sub

I usually don't worry about alternative solutions,
if I got something working.
P.s also, I dont get this problem
while i insert text before the range using
"insertbefore" funciton. Why is that??

No idea. It is by design. Not a bug, a feature.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ

Actually the alternate procedure normally used is to collapse to the end,
the range or selection you are going to insert after. The act of insertafter
method makes it part of range of the range or selection you are inserting
after which starts at the collapsed point and ends at the end of the
inserted text. Now you can format that new range.

For example select the word red with the color red then:

Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertAfter "blue"
Selection.Font.Color = wdColorBlue
 
V

vbaNOOB

Sorry for the late reply. I'm busy with other thing these days

For the sub that u provide, it's work. Thanks

I found out that usually there is space between 2 words. If the space got
formatting,
then the inserted text will carry the space formatting. Am I rite?
That's headahance
 
H

Helmut Weber

Hi vbaNOOB,

Sub Test601A()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "lazy"
While .Execute
rDcm.Characters.Last.Next.Font.Reset
rDcm.Characters.Last.Next = " not "
Wend
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

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