|
From: Mona on 6 Jul 2008 13:15 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
From: Jean-Guy Marcil on 7 Jul 2008 12:20 "Mona" wrote: > 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.
|
Pages: 1 Prev: Drop down list Next: Word 2003/2007 Prompting To Save Document |