Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

How to preserve the user's clipboard contents?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benjamino5 - 14 Sep 2006 21:05 GMT
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
Karl E. Peterson - 14 Sep 2006 21:12 GMT
> My template needs to use Selection.Range.Copy and other clipboard
> operations.
[quoted text clipped - 10 lines]
> 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.
Signature

Working without a .NET?
http://classicvb.org/

Benjamino5 - 14 Sep 2006 21:30 GMT
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

> > My template needs to use Selection.Range.Copy and other clipboard
> > operations.
[quoted text clipped - 15 lines]
>
> It's an impossible task, given Windows support of "custom" formats.
Jonathan West - 14 Sep 2006 21:45 GMT
> Karl,
>
[quoted text clipped - 7 lines]
> 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.

Signature

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

Karl E. Peterson - 14 Sep 2006 21:46 GMT
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

> Karl,
>
[quoted text clipped - 31 lines]
>> Working without a .NET?
>> http://classicvb.org/

Signature

Working without a .NET?
http://classicvb.org/

Benjamino5 - 14 Sep 2006 21:56 GMT
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

> Hi Ben --
>
[quoted text clipped - 51 lines]
> >> Working without a .NET?
> >> http://classicvb.org/
Karl E. Peterson - 14 Sep 2006 22:02 GMT
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,
>
[quoted text clipped - 68 lines]
>> Working without a .NET?
>> http://classicvb.org/

Signature

Working without a .NET?
http://classicvb.org/

Jay Freedman - 15 Sep 2006 01:44 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Karl,
>
[quoted text clipped - 27 lines]
>>
>> It's an impossible task, given Windows support of "custom" formats.
Karl E. Peterson - 15 Sep 2006 01:52 GMT
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.
Signature

Working without a .NET?
http://classicvb.org/

Jonathan West - 15 Sep 2006 09:35 GMT
> My template needs to use Selection.Range.Copy and other clipboard
> operations.
[quoted text clipped - 12 lines]
> 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.

Signature

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

Benjamino5 - 15 Sep 2006 20:32 GMT
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
Jonathan West - 15 Sep 2006 22:04 GMT
> Hi all,
>
[quoted text clipped - 16 lines]
>
> 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.

Signature

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

Benjamino5 - 15 Sep 2006 22:27 GMT
Yes, thanks--I wasn't aware of the .FormattedText property, but now I'm going
to use it quite a lot...
Russ - 09 Oct 2006 01:51 GMT
Another option might be to store formatted 'stuff' as an AutoTextEntry of
the document or template.

>> Hi all,
>>
[quoted text clipped - 21 lines]
> can do so entirely without touching the clipboard, by using the
> FormattedText property. Take a look at it in the VBA Help.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.