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 / February 2008

Tip: Looking for answers? Try searching our database.

Word 2003 macro to re-place original clipboard contents

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alan Stancliff - 20 Feb 2008 23:11 GMT
I have a WORD 2003 macro that uses the clipboard to do various things.
When the macro finishes, the clipboard contains a bit of the stuff the
macro put into it. I would like the macro to re-place whatc had
originally been in the clipboard. What I would like to have happen is this

****SEMI PSEUDO CODE****************
Sub DummyMacro()
' at beginning of macro copy
' clipboard material into variable
Dim myOriginalClipboardVariable as string
myOriginalClipboardVariable:= clipboard_contents
'
'Here macro does a bunch of stuff. A
'At the end of the macro, just before exiting
'clear the clipboard
clipboard:= ""
' then place stuff that was originally in the clipboard back
clipboard := myOriginalClipboardVariable
End Sub
******END SEMI PSEUDO CODE*********

I realize that this will not work as I have written it, but that's
because I am not yet familiar enough with VBA basics.

Would anyone mind giving me a hand here?

Regards,

Alan Stancliff
Jay Freedman - 21 Feb 2008 01:13 GMT
>I have a WORD 2003 macro that uses the clipboard to do various things.
>When the macro finishes, the clipboard contains a bit of the stuff the
[quoted text clipped - 25 lines]
>
>Alan Stancliff

Take a look at http://www.word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm
for methods to get text from the clipboard and to put text there.

Be careful, though: The clipboard can hold lots of other kinds of things besides
text (graphics, OLE objects, etc.). If the user had something other than text
there before the macro started, you won't be able to preserve it through VBA
manipulations.

My recommendation would be to change your macro to do its stuff without using
the clipboard. You can copy a range from one place to another, even between
documents, by assigning the .FormattedText property of the source range to the
.FormattedText property of the destination range. It doesn't have to be just
text, despite the name. To see an example, set a bookmark named bk in any
document covering any part, including pictures, pasted-in spreadsheets, etc.
Then run this macro on it:

Sub demo()
   Dim NewDoc As Document, OldDoc As Document
   Dim SrcRg As Range, DestRg As Range
   
   Set OldDoc = ActiveDocument
   Set NewDoc = Documents.Add
   
   Set SrcRg = OldDoc.Bookmarks("bk").Range
   Set DestRg = NewDoc.Range
   
   DestRg.FormattedText = SrcRg.FormattedText
End Sub

The new document will now be populated with the contents of the bookmark,
without touching the clipboard at all.

--
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.
Alan Stancliff - 21 Feb 2008 03:09 GMT
Thanks Jay,

Than link provided the information I needed.

I appreciate your concern about the clipboard. This macro will be used
by me and a few coworkers during the course of our work day as medical
transcriptionists, and we never would have anything on the clipboard
apart from text. A coworker asked me to fix a macro I had given to her
so that she could have text on the clipboard, run the macro, and still
have the text available. I looked at it as a learning opportunity, and
you provided me with the source of information. My macro now does what
she asked for.

So thanks again.

Regards,

Alan

> Take a look at http://www.word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm
> for methods to get text from the clipboard and to put text there.
[quoted text clipped - 9 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
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.