Copying paragraphs including formatting.

M

Malik Al-Amin

Hi all,

I know how to copy over the contents of a paragraph to a new document.
However my text is copied over but looses all of the formatting. How can I
copy and at the same time keep the formatting, including any blank lines
between sentences? Any ideas?

Thanks in advance.

Malik
 
J

Jean-Guy Marcil

Malik Al-Amin was telling us:
Malik Al-Amin nous racontait que :
Hi all,

I know how to copy over the contents of a paragraph to a new document.
However my text is copied over but looses all of the formatting. How
can I copy and at the same time keep the formatting, including any
blank lines between sentences? Any ideas?

See the FormattedText property.

Try this silly example on formatted text to see how it works:

'_______________________________________
Dim MyRange As Range

Set MyRange = Selection.Range.FormattedText

Selection.Collapse wdCollapseEnd
Selection.TypeParagraph
Selection.FormattedText = MyRange
'_______________________________________

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

Malik Al-Amin

Is there a way to achieve the same thing using the "range" object. I chose
to use the range object instead of the selection object in my solution.
Since I'm using the range object i don't have anything "selected". How do I
get around this limitation?

Thanks
 
J

Jezebel

The Selection object is just a special case of Range object. For most coding
purposes Selection and Ranges are interchangable.
 
M

Malik Al-Amin

Thanks Jezebel

Is it more appropriate to use one over the other? Basically what I'm doing
is I have document1 open and I run a macro that opens up a document2. This
macro copies certain paragraphs from document one and certain paragraphs
from document two and merges them together in document3. I was using the
range object as I was under the impression that this was the object to use
if you're not highlighting anything in the document interface. is my logic
wrong? When merging things back together I want to keep the original
formatting of the paragraphs from their original sources. It was explained
how to do this via the selection object. Is that the way I should be heading
with this?
 
J

Jean-Guy Marcil

Malik Al-Amin was telling us:
Malik Al-Amin nous racontait que :
Thanks Jezebel

Is it more appropriate to use one over the other? Basically what I'm
doing is I have document1 open and I run a macro that opens up a
document2. This macro copies certain paragraphs from document one and
certain paragraphs from document two and merges them together in
document3. I was using the range object as I was under the impression
that this was the object to use if you're not highlighting anything
in the document interface. is my logic wrong? When merging things
back together I want to keep the original formatting of the
paragraphs from their original sources. It was explained how to do
this via the selection object. Is that the way I should be heading
with this?

I think you are doing the right thing, especially if you are manipulating
many bits of texts from one document to another. In those cases, Selection
can lead to all kinds of troubles. Generally speaking, I avoid the Selection
objects, except in short simple projects or in cases where I have no choice
(Information Property, Pre-defined Bookmarks, etc.)

Just use something like:
'_______________________________________
Dim Doc1 As Document
Dim Doc2 As Document
Dim MyRange As Range
Dim MyTarget As Range

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Open("C:\MyDoc.doc")

Set MyRange = Doc1.Paragraphs(2).Range.FormattedText
Set MyTarget = Doc2.Range

With MyTarget
.Collapse WdCollapseEnd
.InsertParagraph
.FormattedText = MyRange
End With
'_______________________________________


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

Jezebel

As far as possible, use Range objects. The Selection object is primarily for
communication between the user and your code (eg, the Selection serves as an
argument to your code). There is almost nothing you can do with the
Selection that can't be done at least as well, if not better, with Range
objects. Range objects also have the advantages that

a) they don't disrupt the user interface
b) they still work even if Word is not visible
c) they make no assumptions at all about how the document is displayed.
(Recorded macros often fail if you try to run them in a different display
mode or with a different kind of selection.)
 
M

macropod

Hi Malik,

One important point to note when copying paragraphs between documents is
that, if the style name used by the paragraph in the source document exists
in the target document, the pasted paragraph in the target document will
take on the same style attributes as already exist there. In other words,
the formatting will change to whatever has been defined in the target
document. Thus, your formatting may still change unless you merge the styles
from the source document to the target document also - but then you'll
change the formatting of any paragraphs already using those styles in the
target document!

Cheers
 
Ö

Örjan Skoglösa

On Wed, 15 Dec 2004 15:54:57 -0500, "Jean-Guy Marcil"
See the FormattedText property.

Try this silly example on formatted text to see how it works:

'_______________________________________
Dim MyRange As Range

Set MyRange = Selection.Range.FormattedText

Selection.Collapse wdCollapseEnd
Selection.TypeParagraph
Selection.FormattedText = MyRange
'_______________________________________

Hi Jean-Guy!

Sorry if this is stupid, but could you explain the last line in an
easy way?

MyRange is a range with formatted text. Why are you able to assign a
range to a property? And why is the range inserted?

TIA
Örjan
 
J

Jezebel

Örjan Skoglösa said:
On Wed, 15 Dec 2004 15:54:57 -0500, "Jean-Guy Marcil"


Hi Jean-Guy!

Sorry if this is stupid, but could you explain the last line in an
easy way?

MyRange is a range with formatted text. Why are you able to assign a
range to a property? And why is the range inserted?

The FormattedText property *IS* a range. But in this case the assignment is
actually a shortcut. If you refer to an object in a context (like this one)
in which an object can't be used directly, VB/VBA looks for the object's
default property. The default property of a range is the .Text property, so
the line is actually a shorthand way of writing

Selection.FormattedText.Text = MyRange.Text
 
Ö

Örjan Skoglösa

Thanks.

I hadn´t realized that a range is a property. But now I see that it
makes sense. (Every object but "the highest one" is either a property
or a method?)

And is .InsertAfter in the same way the default method for the
Selection object?

TIA
Örjan
 
J

Jezebel

A Range isn't necessarily a property -- but a property can have any type,
including object. In this case, the FormattedText property has the type
'Range'.

You're sort of right about every object but the topmost being a property of
something else. This is called the 'object model' -- Microsoft used to
provide maps of the object hierarchy as part of the documentation. It's
probably still there somewhere.

An object is never a method.

Not sure what you're getting at with InsertAfter -- no, it's not the default
method.
 
Ö

Örjan Skoglösa

Ok, then I got it wrong. I thought every object had to be either a
property or a method. I will read up on this.

Thanks
Örjan

PS. That .InsertAfter is about code that I neither do really
understand. I find it strange that
Selection = "abc"
automatically inserts the "abc" part.
 
J

Jezebel

Örjan Skoglösa said:
Ok, then I got it wrong. I thought every object had to be either a
property or a method. I will read up on this.

Thanks
Örjan

PS. That .InsertAfter is about code that I neither do really
understand. I find it strange that
Selection = "abc"
automatically inserts the "abc" part.

The Selection object refers to a section of the document. If you set the
content of the Selection, you are replacing that part of the document.
 

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