How to preserve the user's clipboard contents?

B

Benjamino5

My template needs to use Selection.Range.Copy and other clipboard operations.

However, any time I call a method that uses the clipboard, I will be wiping
out the *user's* own clipboard contents!

I'd like to write a sub that will take whatever's in the clipboard--text AND
pictures AND MathType objects, etc.--and store it somewhere. Then, after my
template uses the clipboard for its own operations, it should restore the
original contents of the clipboard.

I imagine this is a common enough problem that many people here have dealt
with it. What do you do? Do you have any code samples I could use?

Thanks in advance!
Ben
 
K

Karl E. Peterson

Benjamino5 said:
My template needs to use Selection.Range.Copy and other clipboard
operations.

However, any time I call a method that uses the clipboard, I will be
wiping out the *user's* own clipboard contents!

I'd like to write a sub that will take whatever's in the
clipboard--text AND pictures AND MathType objects, etc.--and store it
somewhere. Then, after my template uses the clipboard for its own
operations, it should restore the original contents of the clipboard.

I imagine this is a common enough problem that many people here have
dealt with it. What do you do? Do you have any code samples I could
use?

Apologize. With humility and deep respect for your user. Or find another
way.

It's an impossible task, given Windows support of "custom" formats.
 
B

Benjamino5

Karl,

Thanks for the response, though it's disheartening. By "custom" formats, do
you mean that there are so many different data types it's not possible to
make a DataObject that could contain them?? Or...?

Here's another tack: can I access the multiple-item functionality of the
clipboard in VBA? I haven't found a way to do it yet, so perhaps there isn't
a way.

Ben
 
J

Jonathan West

Benjamino5 said:
Karl,

Thanks for the response, though it's disheartening. By "custom" formats,
do
you mean that there are so many different data types it's not possible to
make a DataObject that could contain them?? Or...?

Here's another tack: can I access the multiple-item functionality of the
clipboard in VBA? I haven't found a way to do it yet, so perhaps there
isn't
a way.

Sorry to pile one item of bad news on another, but Microsoft neglected to
make the Office multiple clipboard feature accessible to VBA.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
K

Karl E. Peterson

Hi Ben --

Windows offers an API called RegisterClipboardFormat, which effectively
allows anyone to define any random binary structure as a custom format. In
theory, you could just store these as binary blobs, then put them back on
just as they were. But you're further complicated by CF_OWNERDISPLAY items,
in which the original application is called upon to draw on demand.

Do you do any plain-vanilla VB? If so, you might find
http://vb.mvps.org/samples/ClipView to be of interest. It enumerates the
formats available, and displays those it understands. You could diddle with
extending it to other formats, to get an idea of how that might be
approached by VB(A). Another demo, http://vb.mvps.org/samples/ClipEx, may
show you how to approach multiple formats through VBA.

But in the end, saving and restoring the clipboard will be a gargantuan task
that will probably never be 100%.

Sorry... Karl
 
B

Benjamino5

Karl,

Thanks for the detailed answer and the links. I've never used VB (though I'd
like to learn), and I now understand the magnitude of this task.

I will follow your original advice--apologize to the users. ;)

Ben
 
K

Karl E. Peterson

Yeah, if they know ahead of time of the impending loss, it's not so bad.
Worst thing is to not tell them, only to let them pin it on you later!
That's what Microsoft did with the VB/VBA IDEs and their add-in interface.
In order for an add-in to add a button to the toolbar, it has to clear the
clipboard. Boy did that ever piss me off, before I learned to anticipate
it! (Still pisses me off, but it's manageable now. <g>)

Good luck...

Karl,

Thanks for the detailed answer and the links. I've never used VB
(though I'd like to learn), and I now understand the magnitude of
this task.

I will follow your original advice--apologize to the users. ;)

Ben
 
J

Jay Freedman

Hi Ben,

While Karl and Jonathan gave you correct answers to the questions you
asked, I think you should ask a different question: What end result
are you trying to accomplish, that might be reached without touching
the clipboard at all?

If you have text/graphics/etc. in one document and you need to make a
copy of it in another document or in another part of the same
document, this is possible.

- Define a Range object that encloses the source content. For example:
Dim Doc1 As Document
Dim srcRg As Range
Set Doc1 = ActiveDocument
Set srcRg = Doc1.Tables(1).Range

- Define another Range object as the destination.
Dim Doc2 As Document
Dim destRg As Range
Set Doc2 = Documents.Add(Template:="MyTemplate.dot")
Set destRg = Doc2.Range

- Transfer the formatted content of the source to the destination:
destRg.FormattedText = srcRg.FormattedText

This completely bypasses both the Windows clipboard and the Office
clipboard, which retain anything the user had stored there.

If that technique doesn't help with your task, there are other ways.
For example, you can use the Save As method to make a copy of the
original file and delete from it whatever you don't want, then use the
InsertFile method to bring the remainder into the destination
document.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
K

Karl E. Peterson

Hi Jay --
While Karl and Jonathan gave you correct answers to the questions you
asked, I think you should ask a different question: What end result
are you trying to accomplish, that might be reached without touching
the clipboard at all?

Yeah, that was part of my initial suggestion -- "Or find another way."

Agreed, and I should have stressed that option, assuming it applies.
 
J

Jonathan West

Benjamino5 said:
My template needs to use Selection.Range.Copy and other clipboard
operations.

However, any time I call a method that uses the clipboard, I will be
wiping
out the *user's* own clipboard contents!

I'd like to write a sub that will take whatever's in the clipboard--text
AND
pictures AND MathType objects, etc.--and store it somewhere. Then, after
my
template uses the clipboard for its own operations, it should restore the
original contents of the clipboard.

I imagine this is a common enough problem that many people here have dealt
with it. What do you do? Do you have any code samples I could use?

To take Jay's question further, so we can explore alternatives that don't
use the clipboard, could you describe in a bit more detail what you are
trying to achieve. In other words

- Where is your data coming from, and what form does it take? (e.g. text,
formatted text, picture etc)

- What are you trying to do with it and where are you wanting to write it?

If both your inputs and outputs are within Word, it is very probable that an
alternative approach exists that doesn't use the clipboard at all. If you
are working on unformatted text it is also very probable that an alternative
approach exists even if you are sending the data outside Word.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
B

Benjamino5

Hi all,

Thanks for the suggestions! I was asking partly because I needed to solve a
particular problem, but mostly because my coworker and I were wondering if
there was a solution we could use in ALL of our macros/templates, so we could
use the clipboard with cheerful abandon in our VBA code whenever we felt like
it.

Anyway, Jay's suggestion of copying ranges works perfectly in solving my
current problem. I was under the mistaken impression that the Range object
wouldn't hold MathType equations, so I would have to use the clipboard. In
fact, it works perfectly.

In case you're curious, the task was simply to take a selection (of text,
graphics, and MathType) and move it into a particular cell of a table.

I really appreciate your advice--I'm all set now.

Ben
 
J

Jonathan West

Benjamino5 said:
Hi all,

Thanks for the suggestions! I was asking partly because I needed to solve
a
particular problem, but mostly because my coworker and I were wondering if
there was a solution we could use in ALL of our macros/templates, so we
could
use the clipboard with cheerful abandon in our VBA code whenever we felt
like
it.

Anyway, Jay's suggestion of copying ranges works perfectly in solving my
current problem. I was under the mistaken impression that the Range object
wouldn't hold MathType equations, so I would have to use the clipboard. In
fact, it works perfectly.

In case you're curious, the task was simply to take a selection (of text,
graphics, and MathType) and move it into a particular cell of a table.

I really appreciate your advice--I'm all set now.

If you want to copy or move formatted text (including objects anchored to
paragraphs within the copied range), within or between Word documents, you
can do so entirely without touching the clipboard, by using the
FormattedText property. Take a look at it in the VBA Help.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
B

Benjamino5

Yes, thanks--I wasn't aware of the .FormattedText property, but now I'm going
to use it quite a lot...
 
R

Russ

Another option might be to store formatted 'stuff' as an AutoTextEntry of
the document or template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top