#1
|
|||
|
|||
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) 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 |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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 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 How can I achieve something like this, please? Save the content, delete it, and then later after more processing write it back? Alex |
#4
|
||||
|
||||
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] |
#5
|
|||
|
|||
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 |
#6
|
||||
|
||||
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 Code:
RangeB.FormattedText = RangeA.FormattedText RangeA.Text = vbNullstring
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Quote:
Code:
Dim RangeA As Range Dim RangeB As Range Set RangeA = Selection.FormattedText Set RangeB = RangeA 'RangeA.Delete Selection.Collapse Selection.FormattedText = RangeB Alex |
#8
|
||||
|
||||
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 |
#9
|
|||
|
|||
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 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 |
|
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 |
copy without using clipboard? | g48dd | Excel | 3 | 07-16-2011 10:28 PM |