Copy a paragraph

L

LEU

I am replacing the cover page on our documents. I am trying to go to a
specific paragraph on the old cover page and copy it. Them I have a macro
that replaces the old cover page with the new cover page. After this happens
I want to past the copied paragraph into the new cover page at the same
location. How do you go to a specific paragraph and copy it and then go to a
specific location in the new document and past it in?
 
G

Gordon Bentley-Mix at news.microsoft.com

Larry,

To make this work, you need two key pieces of information: a way to identify
the source paragraph and a way to identify the target location. The target
location should be easy enough since (I assume) you have full control over
the content of the "new" cover page (an AutoText entry or some such?). If so,
then you should be able to insert a bookmark at the target location and use
something like:

If ActiveDocument.Bookmarks.Exists("TargetBookmark") Then
ActiveDocument.Bookmarks("TargetBookmark").Range.Text = MySourceContent

This assumes that the bookmark in the target location is called
"TargetBookmark" and the cover page paragraph has been stored in a String
variable called "MySourceContent".

However, finding the paragraph in the "old" cover page to use to set the
value of "MySourceContent" may be a bit trickier. Is there something unique
about this paragraph that would allow you to identify it easily? For example,
is it always the first or fourth or tenth paragraph in the document? Or
perhaps it always starts with the same several words? Or maybe it has a
particular style associated with it? Or there's something special about the
paragraph before it (like it says "Title")?

For the sake of argument, let's say that it's always the third paragraph in
the document. In this case you could use something like:

MySourceContent = ActiveDocument.Paragraphs(3).Range.Text

Then after you run your code to replace the old cover page with the new one
(which contains the bookmark for identifying the target location), you call
the first bit of code I posted above.

If all else fails you could possibly do something like set the cursor
manually at the start of the source paragraph and then invoke your "cover
page replacement" routine from there. In this case, all you would need to do
is expand the selection to incorporate the entire paragraph (Selection.Expand
wdParagraph) and then use Selection.Range.Text to set the value of
"MySourceContent".

I'll have to leave it up to you to figure out how to uniquely identify the
source paragraph, but hopefully this will get you started. Post back if you
do find a way to ID the source para but can't work out how to get the content
via code.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Greg Maxey

Understanding that it might be a state secret, but would you be willing to
share the macro that replaces the coverpage? Since a Word document doesn't
really consists of pages until it is presented (e.g., on screen or printed
paper) it is a little difficult to grasp precisely what it is that you are
after. Are these cover pages some sort of AutoText (Building Block) that
you are inserting in the document? Is each cover page in each document
different? I mean is there a reason that you don't put the correct
paragraph in the cover page before you replace it?

Without some more details and the absence of some marker in these old cover
pages I can only offer the possibility of copying some indexed paragraph in
the document:

e.g., ActiveDocument.Paragraphs(8).Range.Copy

Which copies the 8th paragraph in the document.

The paste to some bookmark or other range reference after you have put in
your new cover page.
 
L

LEU

This is what I did that works:

Dim pgraph As String
ActiveDocument.Paragraphs(50).Range.Select
pgraph = Selection

Then after I replace my cover page I run this:

ActiveDocument.Paragraphs(50).Range.Select
Selection = pgraph

If in the first part of the macro I want to copy paragraphs 50 through 53,
how would I change it? I tried Paragraphs(50 - 53), but that gave me an error.
 
G

Gordon Bentley-Mix at news.microsoft.com

Larry,

You can (and probably should - at least from a speed and appearance
standpoint) do all of this without selecting anything. Use ranges instead.

Try something like this:

Sub ReplaceCoverPage()
Dim myRange As Range
Dim myContent As String
Set myRange = ActiveDocument.Paragraphs(50).Range
myRange.End = ActiveDocument.Paragraphs(53).Range.End
myContent = myRange.Text
'Run the code to replace your cover page
ActiveDocument.Paragraphs(50).Range.Text = myContent
End Sub

It could probably be done by extending the selection to the end of paragraph
53, but this is a lot more elegant.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Greg Maxey

LEU,

Dim oRng as Word.Range
Set oRng = ActiveDocument.Range
oRng.Start = ActiveDocument.Paragraphs(50).Range.Start
oRng.End = ActiveDocument.Paragraphs(53).Range.End
 
L

LEU

Gordon,

Thanks for your help. That took care of it. I have a bookmark that I want to
copy the text from the old cover page and fill the bookmark in the new cover
page. So I did the following:

Dim myRanges As Range
Dim myprotitle As String
Set myRanges = ActiveDocument.Bookmarks("bkProTitle").Range
myprotitle = myRanges.Text

'Run the code to replace my cover page

If ActiveDocument.Bookmarks.Exists("bkProTitle") Then
ActiveDocument.Bookmarks("bkProTitle").Range.Text = myprotitle
End If

It puts the text where my bookmark is in the new cover page, but I lose the
bookmark. How do I keep the bookmark "bkProTitle"?


LEU
 
G

Gordon Bentley-Mix at news.microsoft.com

Larry,

Just modify your code as follows:
Dim myRanges As Range
Dim myprotitle As String
Set myRanges = ActiveDocument.Bookmarks("bkProTitle").Range
myprotitle = myRanges.Text

'Run the code to replace my cover page

If ActiveDocument.Bookmarks.Exists("bkProTitle") Then
Set myRanges = ActiveDocument.Bookmarks("bkProTitle").Range
myRanges.Text = myprotitle
ActiveDocument.Bookmarks.Add "bkProTitle", myRanges
End If

This will effectively restore the bookmark after you insert the content, and
it should be safe to reuse your Range object. However, if you're
uncomfortable with that or if it starts giving you problems, you can always
declare a new Range object in the If statement and use that one instead.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
L

LEU

Gordon,

That work perfect. Will this also work for bookmarks in the header or in a
table?

LEu
 
G

Gordon Bentley-Mix at news.microsoft.com

It should do Larry. AFAIK, the Bookmarks collection contains all of the
bookmarks in the document regardless of which story they're in. I regularly
update the content of bookmarks located in various parts of the document -
including headers, footer and tables - using a similar method, and I've yet
to run into a situation when the it doesn't work. The nice part is that
because you're not selecting anything, you don't have to worry about the view
changing if you want to work with something in the header or footer.
Remember: ranges are your friend! ;-D
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Gordon Bentley-Mix at news.microsoft.com

Larry,

Greg is a bit behind the times. <g>

Actually, there's a problem with the newsgroups at the moment. Some posts
are getting delayed, so sometimes you might see a reply that looks to be
redundant. In this case, it really _is_ the machine and not the operator.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
G

Greg

Yes and who could compete with Gordon Bentley-Mix for the affections of those
he assists ;-)
 
G

Gordon Bentley-Mix at news.microsoft.com

Greg! I'm touched! Maybe flowers next time?
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 

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