Issue with the PasteSpecial method for Selection object

Hello all, I want to write a macro in VBA to do the following:
A user has two MS Word documents open with text in both. The user first copies information from document one onto the clipboard. Then, the user goes to document two and places his cursor in a preferred location. Upon running the macro, the information will paste into the second document as a hyperlink back to the first document where the original information was copied.

I started with the attached code. It only works some of the time. Other times, I just get errors saying the clipboard is invalid or empty.

Any help is greatly appreciated!

Thanks.

With Selection.PasteSpecial Link:=True, DataType:=wdPasteHyperlinkEnd with

My first thought is that the copy process is not being correctly done... how is the copy process being performed? Ctrl-C? Edit, Copy? Right-click Copy? And how is the text to be copied being selected? With the mouse? Keystrokes? Please advise.

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

For the most part, the copying is being done by use of the keyboard shortcut Ctrl+C. It's weird: I had something that was nearly working flawlessly yesterday, and I come back to it today, and it's giving the errors again. I don't understand.

Also, search for all instances of normal.dot on the hard drive and rename them.

Also, it may be that a third-party add-in is causing the problems. I've asked the other MVPs and they have advised that some other products can "hijack" the clipboard.

If you want to test this theory, you can remove all files from the startup folder, and see if the problem continues. Then you can put them back into the startup folder one by one until you find the culprit.

You also say that "you come back today and it's giving errors again"... were there updates, changes to the network overnight that may affect it?

Please keep us posted.

0

jpo645Author Commented: 2008-07-29

This is a computer on an Air Force base -- it gets updated all the time and I have little control over that. Although these updates may be causing the issue, it seems unlikely. The macro I'm trying to make is actually a recreation of an earlier macro from our old word 2003 toolbars; and, if you use those old toolbars in Word 2007 (though, it looks like crap), the macro works perfectly. But when I try to port it over to 2007 (namely, by renaming the macro as an IRibbonControl) then I get the clipboard problem.

So, given this porting issue, I decided I'd recreate it; the first day it was working fine, but I come back the next day (after rebooting the computer and whatnot) and clipboard problem returned. If I make a new macro with my code, it will generally work until I flip my computer off for the night. This makes almost no sense.

I will try to see if there are things in the startup folder that are causing the issue.

I can change the wdPasteHyperlink to any of the other possible constants and it works fine. It really doesn't like pasting hyperlinks!

You know, I've installed Office 2007 and ever since, I find that every now and then, my clipboard is empty, even though I JUST copied something.... I'm going to report it to Microsoft and ask them to look into it. This appears to be what's happening to you as well.

"It only works some of the time. Other times, I just get errors saying the clipboard is invalid or empty."

So does it happen EVERY time or just sometimes?

0

jpo645Author Commented: 2008-08-19

It works a small minority of the time. If I take the code and start from scratch, it will most likely work the first few times I run the pastespecial command. However, if I close the VB session and exit Word, most likely, it will stop working thereafter. Also, if I reboot the computer (which I always do at the end of the day, per our work policy) it will most assuredly stop working. Sometimes, when I play around with the code, I can get it to work haphazardlybut not consistently: in fact, the only thing Ive found to be consistent is that it doesnt work. But Im taking code almost directly from another Macro that does work (although, not in the way I would like it to), so I'm still really confused. I will post the macro that does work when I geta hold of it.

How are you making the selection? Perhaps that's the issue.... because the code you posted is just to paste the text as a link.... what is the code that does the copying and how is the selection made?

0

jpo645Author Commented: 2008-08-19

So the intended macro is supposed to make a circular refernce between two documents.

You have two documents open. In document 1, the user selects the text and copies it. Typically, I select the text with my mouse and do a CTRL + C. Then the user goes to the destination document and runs the macro in the spot where they want the link to go. The macro sets up a bookmark in the destination document for the original document to link to, then goes back, using hyperlink.follow, and sets up a bookmark for the destination document to link to.

Recently, I upgraded all our old macros to work for Office 2007 by allowing the subs to accept IRibbonControls and placing them as buttons on a custom tab. From the onset of this change, the macro stopped working. And it stopped working specifically at the pastespecial method, posted above. When I tried to rewrite the macro from scratch, I started simply with the pastespecial method to test. And just the method by itself, without any other code, gives me the same error. Which is why it is the only thing I posted.

That said, if you can think of another way to do this (and I'm sure you can) without even using pastespecial I'm certainly open to any ideas.

Thanks for your help and patience.

0

jpo645Author Commented: 2008-08-19

So I think I solved part of the issue. The documents I test this on are usually new documents that have not been saved. As such, Word didn't know what to link to. However, I am working from home now, and will see if the macro works tomorrow morning for this reason.