PDA

View Full Version : Solved: Duplicate formatted text



Adamski
06-05-2009, 11:14 AM
Is there a way to duplicate the formatted text in a range to another range without using the clipboard?

I tried setting the new range to OldRange.Duplicate but the formatting is lost. It is easy with copy-paste but that removes the original clipboard content which I would like access to afterwards.

I also considered trying using the clipboard pane which can store 24 items but cant figure out how to use it with code. Is it possible to copy the range to item 2 in the list with vba, leaving the original contents intact and then pasting item 2 to a new range?

fumei
06-05-2009, 11:38 AM
"Is there a way to duplicate the formatted text in a range to another range without using the clipboard?"

Yes, use Styles, which is how Word is designed to be used.

"I tried setting the new range to OldRange.Duplicate but the formatting is lost. It is easy with copy-paste but that removes the original clipboard content which I would like access to afterwards."

Why? I am not disputing that you may need the content, but is there not perhaps a better way? Putting the content into a string variable? i do not know, as I do not know the context.

"Is it possible to copy the range to item 2 in the list with vba, leaving the original contents intact and then pasting item 2 to a new range?"

I do not understand what you mean by this. I do not think it is possible to copy anything (replace) to a specified item in the list. That is:

Item 1: "yadda yadda"
Item 2: "blah blah"

Do something to change Item 2.

Item 1: "yadda yadda"
Item 2: "mary had a little lamb"

Further, I do not understand WHAT you are refering to by "original contents".

It is very possible, and IMO, the best way to do it to "copy" a range to another range with ever doing any copy/paste whatsoever.
Sub YaddaText()
Dim r1 As Range
Dim r2 As Range

' ensure selection point collapsed
Selection.Collapse 0

Set r1 = Selection.Range
Set r2 = ActiveDocument.Paragraphs(14).Range

r1.Text = r2.Text
End Sub
The text of paragraph(14) is "copied" to the Selection point without any copy and paste functions.

True...the format will NOT be carried with it. Which is why we use Styles.
Sub YaddaText()
Dim r1 As Range
Dim r2 As Range
Dim OriginalStyle As Style

' ensure selection point collapsed
Selection.Collapse 0

Set r1 = Selection.Range
Set r2 = ActiveDocument.Paragraphs(1).Range
Set OriginalStyle = r2.Style

With r1
.Style = OriginalStyle
.Text = r2.Text
End With
End Sub
Now the style of the first range IS duplicated into the other.

fumei
06-05-2009, 11:39 AM
I do want to add that if you are not using Styles, then my post above are rather moot.

macropod
06-05-2009, 11:22 PM
Of course, you could just bookmark the first range, then insert a cross-reference to the bookmark wherever you need the bookmarked range to be replicated. The formatting will be carried across, too. With a cross-reference, any changes to the bookmarked range will (eventually) update the cross-reference. If you want to, though, you can unlink the cross-reference so that its text becomes static.

fumei
06-08-2009, 09:36 AM
Excellent comment, and to take that further, you could use INCLUDETEXT to either get the text from a bookmark in the same document, or a bookmark in another document.

The point being is, what - EXACTLY - are your requirements?

Adamski
06-08-2009, 10:53 AM
Thanks for the ideas.

I have some code to format a section - size, margins etc. Part of this cuts everything from the header and pastes it leaving an empty header.

This all works fine, but as soon as a user got to try it they:
1. copied an image
2. run the code to alter the page
3. tried to paste the image

Problem is that the image is gone, and they actually paste the old header which is still on the clipboard. Ideally I want the clipboard to still contain the image (or whatever) after the code runs. Failing that, it would be better to have the clipboard emptied so they don't paste the header.

Bookmarking and cross-referencing sort of works but I can't get the last paragraph mark in the bookmark. Could just put an extra paragraph in before bookmarking, but also I have fields in there which are unlinked if I unlink the cross-reference before deleting the header content.

Not yet tried INCLUDETEXT.

Hope it makes sense...

Adam

fumei
06-08-2009, 12:01 PM
1. Do not get them to copy first.

2. why is the user doing anything at all?

3. seems that they may be some usefulness to using Autotext perhaps.

4. "I have some code to format a section - size, margins etc." Why? Why is this being done via code?

Adamski
06-11-2009, 05:36 AM
1. I can't control how users work
2&4. It's just part of a custom toolbar to help users out. It inserts a new section and changes it to landscape to give them more room for wide tables and images.

Anyway, I have now decided clearing the clipboard af the end of the code is good enough. They can copy whatever again when they realise its gone.

I have used code which dosen't require the DataObject (unlike the one in the kBase).


Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Option Explicit

Sub ClearClipboard()
' Clears the Clipboard
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Sub