Jay
Wed Feb 20 17:13:04 PST 2008
On Wed, 20 Feb 2008 15:11:05 -0800, Alan Stancliff
<alanstancliff@nojunkyahoo.com> wrote:
>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.
--
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.