Preserving clipboard on VSTO load - c#

I've written a small application as a test to see what Excel VSTOs are capable of. However, I've run into a problem.
When I start Excel my clipboard is cleared. I've traced this down via Google to be a problem with Add-Ins (specifically COM add-ins) that when not initialized correctly will clear the clipboard automatically. Unfortunately for me I don't know how to preserve the clipboard in a VSTO and Google isn't much help. I don't call any functions that would obviously clear the clipboard. The only thing in the ThisAddin function is an if/else block that reads from some properties and sets some flags.
I've mulled over the idea of copying data on the clipboard to some object and then just replacing this but that seems very hacky and probably won't work.
Can anyone shed some light on how/why my VSTO clears the clipboard when Excel starts and how I can remedy it?
EDIT:
After tracing a little bit deeperI was wrong about it being my person test add-in. I figured out that it was either the Team Add-In or Adobe Contribute. Given Adobe's track record with these things I would go ahead and say it was probably the contribute add-in. Thanks everyone for your replies. This will teach me for using a non-vanilla installation of Excel for VSTO practice.
I will leave this topic open so that more people can contribute to possible reasons why a VSTO Excel Add-In (or any Add-In for the office suite) would clear the clipboard on start.

It would be best to identify the addin that's trashing the clipboard and removing it.
If that's not an option checkout some of the answers in Chris' link. I've done this using the Win32 API before. I think what's lacking in the listed answers is calls to OpenClipboard( http://msdn.microsoft.com/en-us/library/windows/desktop/ms649048%28v=vs.85%29.aspx) and CloseClipboard.

It's folly to try to backup/restore the clipboard, when complex data types are present. And you will generate unwanted clipboard events. See my prior answer here:
https://stackoverflow.com/a/2579846/289135

The COM addin Adobe PDF Maker, Bluetooth module clears the clipboard when Excel starts. Disable it to stop this behaviour.

Related

How to programmatically sign the code in a Word Document [duplicate]

Is there a way to programmatically add a digital signature to a VBA Macro in a word document using C# or VB.Net?
There are some similar questions asked already; however, none that I could find answer my question:
How to programmatically remove the digital signature from VBA macros in Excel?
Digital Signature for Excel Macro
Unfortunately, there isn't an automation API for this in the VBA environment. I got it working 100% unattended (so I could use it in automated builds) by P/Invoking Win32 APIs to grab the right window handles and simulate the keyboard shortcuts to those windows, highlight the right cert, etc. Even had it working to detect and capture compilation errors (grab the error dialog window and dig out the label control text). It took me a few days of hacking to get it working reliably, but it is possible, and really not even that hard.
I'd be happy to post it, but my previous employer owns the code. :(

How to Access the Undo Stack in Excel from C# via Office Interop?

I'm building a C# app that needs to interact with an Excel spreadsheet via Office Interop. I'd like to gain access to the full Undo stack so I can make a series of changes to the worksheet and then roll them back one at a time if necessary. I see that the Word Document object in the Interop assemblies has UndoRecord and some other niceties, including an Undo function. For example, I can call Microsoft.Office.Interop.Word._Document.Undo() with a parameter to undo multiple actions. Excel's Undo stuff, on the other hand, looks much more limited. Sure, there's Microsoft.Office.Interop.Excel._Application.Undo(), but it goes only one action deep, and if called again performs a "Redo" instead of going deeper down the stack.
I've found code online where VBA developers coded their own Undo histories for Excel because that was the only way to get the desired functionality. I don't see anything like this for C#, though. Is there any way to mimic the way Word's Interop undo works in Excel?
Alas, what's true for Word isn't for Excel. Probably because so many more people write Word VBA :). See this John Walkenbach page for what is possible in Excel VBA (and interop). Here's part of his description:
Making the effects of your subroutines undoable isn't automatic. Your
subroutine will need to store the previous state so it can be restored
if the user choose the Edit Undo command. How you do this will vary,
depending on what the subroutine does. In extreme cases, you might
need to save an entire worksheet. If your subroutine modifies a range,
for example, you need only save the contents of that range.
EDIT:
There is the Application.Undo method, but it's very limited. Here's the description from Excel 2010 VBA help:
Application.Undo Method
Cancels the last user-interface action.
Remarks This method undoes only the last action taken by the user
before running the macro, and it must be the first line in the macro.
It cannot be used to undo Visual Basic commands.
In regards specifically to Interop, I'm sure there's nothing available that's not in VBA itself.

Integrating C# code in Excel

I have an Excel file with many different, but very similar, userforms.
When I click on a cell in my worksheet a specific userform will open according to the cell text.
I wanted to know if there is a way to add a C# form to Excel which will behave as a userform? Since the visual studio interface is much simpler and a lot more generic than VB.
I know you can create C# add-in's for excel, but these are used to add buttons, ribbons, etc.
what I need is a way to add a dll which will be activated when clicking on a cell, this dll must be easy to update, and it should not include installation (like the add-in's).
I got the following answer from a different forum:
You have some possibilities
1) Actually Excel (and practically all Office application) support Add-ins. Thus you could make a legacy excel add-in. There are several tutorials out there, but this is not the simplest one.
2) An other interesting approach exceldna.codeplex.com/[^]
3) You can call managed code from excel vba, but it is not the wisest one, since vba runs in a rather undeterministic way. Start here: richnewman.wordpress.com/2007/04/15/…^]
4) Interop Forms Toolkit[^] might be also an alternative – user2134909 2 mins ago edit
I haven't thoroughly examined them, but they seem promising

Undo action for Excel addin

How does undo work in Interop.excel? C# - making a addin in VS
I imagine the following:
I register a undo-method on the stack (implemented by me).
I save the current state... where?
When the user uses undo (ctrl-z), my undo-method gets called, and I restore the previous state with the data that I stored.
Can't get any good info on this though. Maybe it works totally different?
You could look at NetOffice http://netoffice.codeplex.com/ They are tutorials how to create your own Add-In. You can work with VSTO, download the NetOffice .dll which you need to write your own Excel Add-In and to design it as you wish and implement your functionalities.

Is it possible to write an Addin that works in both Excel 2003 and 2007?

I've been writing a shared addin for Excel 2003. It adds a menu (CommandBarPopup) and a couple menu items off it (CommandBarButton) that when clicked open a WPF window to read a couple values and manipulate the worksheet.
Is there a way to write it so that it would work in office 2007/2010 as well, or is it impossible?
If so, can anyone provide instructions for how that is done?
MS-Office libraries are quite well back-compatible, so your addins should work in future version just as they are.
This really shouldn't be trouble at least for commandbarbuttons and popups. If you're about to do something harder, I strongly recommend Add-in Express (sorry for advertising, I am just a satisfied customer ;-) )
Pz, the TaskConnect developer

Categories

Resources