Link to home
Start Free TrialLog in
Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on

Word VBA: Copy contents of one rich text content control to another without losing formatting?

I'm using Word 2010.  Trying to use Word VBA to copy the contents of one rich text content control to another rich text content control in a second document.  Using this method below.  But the destination CC does not retain the formatting (specifically list numbering, bold, italics) that were in the source CC.  
TargetReport.SelectContentControlsByTitle("Materials").Item(1).Range.Text = _
SourceProtocol.SelectContentControlsByTitle("Materials").Item(1).Range.Text

Open in new window

Below is the result I get.
User generated imageUser generated imageDo I need to be using a different method to accomplish this without losing the formatting?

Thanks!
Avatar of Bryce Bassett
Bryce Bassett
Flag of United States of America image

ASKER

Well, maybe the simplest solution is the best in this case.  If I select and copy the range of the first content control and paste it into the second, and it brings over the formatting just fine.  
SourceProtocol.SelectContentControlsByTitle("Materials").Item(1).Range.Select
Selection.Copy
TargetReport.SelectContentControlsByTitle("Materials").Item(1).Range.Select
Selection.Paste

Open in new window

Any reason I can't just do it this way?  Is there a more robust method?
That is fine, attempt 1 just sets the text, not the text attributes
The only reason to avoid copy paste is if you don't want to smite the clipboard, so might not be popular if you are giving to others to use. Fine for personal use though
Well, this is in fact for client use, so if there's a way to avoid the clipboard that would be nice.

I can save a couple lines by shortening to:
SourceProtocol.SelectContentControlsByTitle("Methods").Item(1).Range.Copy
TargetReport.SelectContentControlsByTitle("Methods").Item(1).Range.Paste

Open in new window

But this still uses the clipboard, doesn't it?  

Is there a direct way to copy and paste in one statement without using the clipboard?  For example, I've used this method in Excel, and assumed this does not use the clipboard, but maybe I'm wrong:
mySheet.Range("A1:D4").Copy Destination:= mySheet.Range("E5")

Open in new window

Any other thoughts would be appreciated.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Subodh Tiwari (Neeraj)
You can use something like this to clear the clipboard...
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


Sub EmptyClip()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Sub

Open in new window

Didn't really answer my question.  I don't need to bother with a custom clipboard.