Manipulate Text Within a Range

A

Andrew

Can someone nudge me in the right direction with manipulating text within a
range? I am searching for text in one document and building a range array.
So far so good. Then I type out the entire array to a new document. Each
element in the array will have more than one paragraph. The first paragraph
of each element needs to have a different sytle than the rest of the
paragraphs in that range element. (Of course, I don't want to affect the
source document.)

There are so many ways to do this, none of which I seem to be able to code.

I'd be happy either:
(1) extracting the first paragraph to a separate range (or selection?)
variable and applying the style to that range variable; or

(2) simply applying the style to paragraphs(1) of the range element before I
type it to the target document; or

I can't even seem to copy the contents of the ranges to a new variable
without continuing ponting to the original text. There's obviously some
fundamental gaps in my knowledge of ranges.

Thanks for your help!
 
J

Jean-Guy Marcil

Andrew was telling us:
Andrew nous racontait que :
Can someone nudge me in the right direction with manipulating text
within a range? I am searching for text in one document and building
a range array. So far so good. Then I type out the entire array to a
new document. Each element in the array will have more than one
paragraph. The first paragraph of each element needs to have a
different sytle than the rest of the paragraphs in that range
element. (Of course, I don't want to affect the source document.)

There are so many ways to do this, none of which I seem to be able to
code.

Something like:

MyRange.Paragraphs(1).Style = "Heading 1"
???

If not, post the code you have so far (the bit about typing the array to a
new document).

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Andrew

Thanks for the response.

rTmp(i).Paragraphs(1).Style = "Heading 1"

applies the "Heading 1" style to the source document only. The target
document style is normal. I don't understand why exactly. Here is the bit
of code that types out the array to the target.

For i = 1 To UBound(rTmp)
Selection.TypeText rTmp(i)
Next

Merci.
 
J

Jean-Guy Marcil

Andrew was telling us:
Andrew nous racontait que :
Thanks for the response.

rTmp(i).Paragraphs(1).Style = "Heading 1"

applies the "Heading 1" style to the source document only. The target
document style is normal. I don't understand why exactly. Here is
the bit of code that types out the array to the target.

For i = 1 To UBound(rTmp)
Selection.TypeText rTmp(i)
Next
Not knowing where and how rTmp() is created, it is hard to give any advice.
I do see you are using the Selection object in code that deals with multiple
documents.... Very hairy and difficult to control, as you have found out.

You want to declare 2 Document variables, as in:

Dim SourceDoc As Document
Dim TargetDoc As Document
..
..
..
Set SourceDoc = ActiveDocument 'At the top of the code
..
..
..
Set TargetDoc = Documents.Add 'When you create/open the second one
Then always use those variables to refer to the documents.
Also, 2 Range variables:

Dim SourceRge as Range
Dim TargetRge as Range
..
..
..
Set SourceRge = SourceDoc.Range 'for example...
Set TargetRge = TargetRge.Range

So from now on, you are sure that TargetRge always refers to the content of
the target document.

Finally, insert the text like this, assuming that rTmp is a String array:
Also, arrays start at 0, unless otherwise specified when they are created.

For i = 0 To UBound(rTmp)
With TargetRge
.Collapse wdCollapseEnd
'Insert the text, and a ¶ if necessary (The Chr(13) inserts a return)
.InsertAfter rTmp(i) & Chr(13)
'Now the range refers only to the stuff that just has been inserted
.Paragraphs(1).Range.Font.Bold = True
End With
Next


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Andrew

Jean-Guy,

Thanks so much for this great information. I will work with this. You
gave me a lot to think about and play with.
 
J

Jean-Guy Marcil

Andrew was telling us:
Andrew nous racontait que :
Jean-Guy,

Thanks so much for this great information. I will work with
this. You gave me a lot to think about and play with.

Glad I could help.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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