Word 2003 macro to re-place original clipboard contents

A

Alan Stancliff

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
 
J

Jay Freedman

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

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.
 
A

Alan Stancliff

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
 

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