copying styles

M

Mona

I am trying to copy a selected section of a document to a newly
created document. I want to be able to specifically copy the custom
style of that selection as well. does anyone know how to do this?

I use this line in my code :
Selection.Bookmarks("\page").Range.FormattedText
For most cases it does copy styles too, but there are cases where it
misses out on the specific sytle and turn a paragraph to a Normal
style. It usually happens with the last paragraph of the selection.

it would be ideal if I could grab the style name of each paragraph
with all its specifications and then assign it to the one that is
being copied into the new document.

thanks for your help
mona
 
J

Jean-Guy Marcil

Mona said:
I am trying to copy a selected section of a document to a newly
created document. I want to be able to specifically copy the custom
style of that selection as well. does anyone know how to do this?

I use this line in my code :
Selection.Bookmarks("\page").Range.FormattedText
For most cases it does copy styles too, but there are cases where it
misses out on the specific sytle and turn a paragraph to a Normal
style. It usually happens with the last paragraph of the selection.

it would be ideal if I could grab the style name of each paragraph
with all its specifications and then assign it to the one that is
being copied into the new document.

When using
Selection.Bookmarks("\page").Range.FormattedText
you have to be aware of a few problems that may arise.

The built-in "\page" bookmark...
1) includes the manual page break at the end of the page if there is one;
2) includes the section break at the end of the page if there is one;
3) does not include the last ¶ character on the last page;
4) if the selection is on the last page, and that page consists of a single
paragraph, an error is generated as the bookmnark cannot be defined in such
cases;
5) if a paragraph wraps to the next page, there will be no Paragraph
information in the .FormattedText property when you paste that range because
the ¶ in on the next page (the same applies to case 3), but for a different
reason...).

Keeping all that in mind, you can modify your code to get what you need.
For instance, if the last character is a break, remove it from the range:

Dim rngPage As Range

Set rngPage = Selection.Bookmarks("\page").Range

If rngPage.Characters.Last = Chr(12) Then
rngPage.MoveEnd wdCharacter, -1
End If

As for the formating issue, you can get the paragraph style bye checking the
last paragrpah:

Dim rngPage As Range
Dim strStyle As String

Set rngPage = Selection.Bookmarks("\page").Range

If rngPage.Characters.Last <> Chr(13) Then
strStyle = rngPage.Paragraphs.Last.Range.Style
End If

etc.
 

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