Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2020, 12:56 AM
alex100 alex100 is offline Replicate the copy/paste function, without involving the clipboard Windows 7 64bit Replicate the copy/paste function, without involving the clipboard Office 2016
Advanced Beginner
Replicate the copy/paste function, without involving the clipboard
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Replicate the copy/paste function, without involving the clipboard

I am using the following code to save some content, then write it to another location inside the document.



Code:
Dim content As String
content = Selection.Text

Selection.TypeText (content)
The problem is the written content no longer preserves the format of the original one. For example, in case of text content, it only saves the text, but without preserving its font size, color, shading, etc. Beside text, the content can also contain images and tables.

So what I need is to replicate the functionality of the copy/paste function, but without messing up the clipboard content (which would happen if I would use the 'Selection.Copy' and then the 'Selection.Paste' instructions).

Alex
Reply With Quote
  #2  
Old 11-08-2020, 03:23 AM
macropod's Avatar
macropod macropod is offline Replicate the copy/paste function, without involving the clipboard Windows 10 Replicate the copy/paste function, without involving the clipboard Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can use the FormattedText property. See, for example:
https://www.msofficeforums.com/word-...inserting.html
https://www.msofficeforums.com/word-...row-paste.html
https://www.msofficeforums.com/word-...aste-same.html
https://www.msofficeforums.com/word-...o-regular.html
https://www.msofficeforums.com/word-...e-another.html
https://www.msofficeforums.com/word-...-document.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-08-2020, 05:50 AM
alex100 alex100 is offline Replicate the copy/paste function, without involving the clipboard Windows 7 64bit Replicate the copy/paste function, without involving the clipboard Office 2016
Advanced Beginner
Replicate the copy/paste function, without involving the clipboard
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Thank you for your suggestion, Paul! Using the 'FormattedText' property, I came up with this code...

Code:
Dim Content As Range
Set Content = Selection.FormattedText
  
Selection.Collapse
Selection.FormattedText = Content
It's working fine, except for one thing... After I copy the content, I'd like to also quickly delete it, and then (after more macro processing) write it back to the document.

Something like the code below, which obviously doesn't work. I'm just writing it so that you can understand what I'm looking for...

Code:
Dim Content As Range
Set Content = Selection.FormattedText

Content.Delete

'
' more processing takes place here...
'

Selection.Collapse
Selection.FormattedText = Content
I tried using two different range names for the content, so when I delete one of them, the other can still be saved. But it didn't work...

How can I achieve something like this, please? Save the content, delete it, and then later after more processing write it back?

Alex
Reply With Quote
  #4  
Old 11-08-2020, 01:40 PM
macropod's Avatar
macropod macropod is offline Replicate the copy/paste function, without involving the clipboard Windows 10 Replicate the copy/paste function, without involving the clipboard Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Why not create a custom undo record? For example:
Code:
Sub Demo()
Dim objUndo As UndoRecord
Set objUndo = Application.UndoRecord
With objUndo
  If .IsRecordingCustomRecord = False Then
  .StartCustomRecord ("Demo")
  'insert whatever processing you want to perform here
  .EndCustomRecord
End With
ActiveDocument.Undo
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-09-2020, 01:59 AM
alex100 alex100 is offline Replicate the copy/paste function, without involving the clipboard Windows 7 64bit Replicate the copy/paste function, without involving the clipboard Office 2016
Advanced Beginner
Replicate the copy/paste function, without involving the clipboard
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Why not create a custom undo record?
Well, this may work too, but the copy/paste function that I'd like to replicate is already integrated in a much larger macro. Switching to another solution would probably involve some other changes to this macro, and I'd like to avoid that as much as possible. Not to mention I never used Undo Record before...

The 'FormattedText' property seems perfect for what I need, only that I also need to be able to delete the original content prior to writing it back to the document, just like the copy/paste function works... you copy something with Ctrl + C, and then you are free to delete that content without automatically deleting it from the clipboard too. That's what I'm looking for.

Alex
Reply With Quote
  #6  
Old 11-09-2020, 02:11 AM
macropod's Avatar
macropod macropod is offline Replicate the copy/paste function, without involving the clipboard Windows 10 Replicate the copy/paste function, without involving the clipboard Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can't store content as 'FormattedText' to a variable. And what your code in post #1 does is store only the unformatted string.

To use the 'FormattedText' property in anything like you describe, you'd have to use something like:
Code:
RangeA.FormattedText = RangeB.FormattedText
then modify RangeB, before restoring it via code like:
Code:
RangeB.FormattedText = RangeA.FormattedText
RangeA.Text = vbNullstring
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-09-2020, 04:02 AM
alex100 alex100 is offline Replicate the copy/paste function, without involving the clipboard Windows 7 64bit Replicate the copy/paste function, without involving the clipboard Office 2016
Advanced Beginner
Replicate the copy/paste function, without involving the clipboard
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
To use the 'FormattedText' property in anything like you describe, you'd have to use something like:
I have no doubt this is the right approach, but I have trouble implementing it in my own code. ;-) Here's what I have so far...

Code:
Dim RangeA As Range
Dim RangeB As Range

Set RangeA = Selection.FormattedText
Set RangeB = RangeA

'RangeA.Delete

Selection.Collapse
Selection.FormattedText = RangeB
The code works ok until I uncomment the 'RangeA.Delete' instruction. If I do that, RangeB is deleted together with RangeA, so in the end nothing will be written to the document.

Alex
Reply With Quote
  #8  
Old 11-09-2020, 06:11 AM
gmayor's Avatar
gmayor gmayor is offline Replicate the copy/paste function, without involving the clipboard Windows 10 Replicate the copy/paste function, without involving the clipboard Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Could you clear RangeA after processing? Without knowing what the aim of the game is it's a bit difficult to judge. Here the formatted range is written to the end of the document and then the original is deleted.

Code:
Dim RangeA As Range
Dim RangeB As Range
    Set RangeA = Selection.Range
    Set RangeB = ActiveDocument.Range
    RangeB.Collapse 0
    RangeB.FormattedText = RangeA.FormattedText
    RangeA.Text = ""
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 11-09-2020, 08:19 AM
alex100 alex100 is offline Replicate the copy/paste function, without involving the clipboard Windows 7 64bit Replicate the copy/paste function, without involving the clipboard Office 2016
Advanced Beginner
Replicate the copy/paste function, without involving the clipboard
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Could you clear RangeA after processing?
I'm afraid not, the space must be cleared before the processing starts.

So far I've been using this code, but like I said, it's messing up the clipboard...

Code:
Selection.Copy
Selection.Delete
'
' processing here...
'
Selection.Paste
Is there any other straightforward solution to replicate this copy/paste behaviour as close as possible?

I also thought about saving the original clipboard content to a variable before 'Selection.Copy', and then restore it back before the end of the macro. That would still be a decent solution, although not that elegant. But the clipboard can also contain binary information (not only text), and I could not find a way to do this in Word VBA. Saving from and writing back to clipboard text information is really no problem.

Alex
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to copy specific formated text from Word and paste from clipboard into a console application Userx Word VBA 0 10-09-2020 05:58 AM
How to paste as a single para from clipboard having multiple paragraph PRA007 Word VBA 8 12-04-2015 04:48 AM
Replicate a function from MS Visio? CamSTAR1 Excel 0 08-20-2013 07:13 PM
If Function Challenge involving date no1texan Excel 6 07-15-2012 12:58 PM
Replicate the copy/paste function, without involving the clipboard copy without using clipboard? g48dd Excel 3 07-16-2011 10:28 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:40 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft