themoosman

I am trying to intercept a user paste in a VSTO application. I know I could use the OnAction parm, but we are trying to use a VBA free application and we have to stay on VSTO 2005 first edition. Using the spy++ application I noticed that the WM_COPY (or WM_PASTE) message is not posted to the Excel workbook, or application when a copy occurs in the application. I have been searching for hours and I cannot find anything. Can anyone shed some light on this problem

Thanks,

Karl



Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

Geoff Darst - MSFT

Hi Karl,

WM_COPY, etc are only sent to Win32 edit boxes (or Combo Boxes) so you won't see those messages as part of a clipboard operation with an application such as Excel. Even if you could hook WM_COPY, that would never tell you that a paste had occured--it would just tell you that something was copied to the clipboard. Can you assume that a paste will always follow In the user case, it is certainly possible to copy something to the clipboard and not paste. If that is an assumption you can make, WM_DRAWCLIPBOARD may work for you. This message will get sent to Excel's clipboard viewer window whenever the clipboard changes. However, this message may get sent when the clipboard is cleared as well which means it is even less precise than something like WM_COPY.

It might help to understand what you are actually trying to accomplish. Perhaps there is another way besides trying to hook the clipboard.

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

chrtop

Hi,

I have a question related to this one; I've been hunting around quite a bit, but I can't quite seem to find the right magic to make what I want to happen happen...

I want to be able to intercept the Paste and Fill Down commands in order to perform some rewriting of the pasted formulae. I would like to intercept this operation whether it came via a menu selection or a keyboard shortcut.

For argument's sake, let's say I want to make it so that when a user performs a paste of a simple string formula (="my value") it is pasted with all words capitalized. All other pastes should behave as usual.

I thought that "window subclassing" might help, but I don't appear to get any useful-looking messages, other than the WM_CLIPBOARDDESTROY and WM_DRAWCLIPBOARD. I'm afraid I might be approaching this the wrong way. Is there some other way to "override" the paste functionality






Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

Geoff Darst - MSFT

Hi,

There really isn't any supported way to intercept clipboard operations in Excel. The only clipboard hooking mechanism provided by the operating system is the clipboard viewer chain. You can see what is put on the clipboard, but you can't tell what applications are doing with it. You also have no way of knowing who updated the clipboard; you can only know that it changed.

The problem is that Excel's clipboard handling is all internal. You might be able to constuct some sort of hacky solution where you tried to intercept all UI gestures that could possibly lead to a paste (and then you would still have to figure out what was pasted where), but any such solution would be fairly brittle and subject to being broken by future versions (and you would just be guessing that you had covered all possible paste scenarios). Any such solution would be highly unsuitable for production code.

Sincerely,

Geoff Darst

Microsoft VSTO Team





Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

chrtop

Hi Geoff,

Thanks for the prompt reply.

That's what I was afraid of.

What we have now is a completely separate copy/paste mechanism that uses different shortcut keys and different menu items. I was hoping to somehow make things work with the usual set of gestures.

It seems that the "hacky" solution is the only remaining solution. I'm able to intercept the keyboard events (CTRL+C, etc.), but I'm not sure how to intercept the menu operation that is triggered as a result of the selecting existing menu items. Any pointers to show how I might do this


Thanks again,






Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

chrtop

I have found out how to override the existing menu items. In VBA:

Code Snippet (override copy action)

CommandBars("Edit").Controls("Copy").OnAction = "MyMacro"



I'll just have to translate this to C++, which should be easy enough I think.







Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

chrtop

Hmm...what's not obvious is how to call the "default" implementation if I decide I don't need to handle the action. OnAction is empty before I set it, so I can't look up the function that way. I suppose I could try changing the OnAction back to empty and them triggering the menu item And after triggering, set the OnAction back to my method That probably wont work, since the system will be busy handling my OnAction at the point where I try to trigger the action...So would the only possibility left be implementing the "default" behavior myself Any ideas





Re: Visual Studio Tools for Office Intercept WM_COPY message in Excel VSTO

Geoff Darst - MSFT

Hi Chris,

I think you are more or less out of luck again (at least as far as the object model goes). AFIK there is no way to call Excel's handler for a given command bar item directly through the object model. Probably what you would need to do would be to subclass the command bar item's window and intercept the appropriate message that indicates the click. At that point you could either eat the message and execute your own code or you could pass the message on to the real WndProc (which would invoke Excel's handler).

Sincerely,

Geoff Darst

Microsoft VSTO Team