Undo action for Excel addin - c#

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.

Related

Share code between office applications

I have written two addins , 1 for excel and 1 for word. However these addins have a lot of duplicates: Database handling, file handling,array handling which I would like to update 1 place instead of two.
We do have access to sharepoint, and could get access to visual studio. The thing is that people like to use file explorer and find the correct word or excel file, then open it then press a button inside the application which then should do things with the active document.
This is why we haven't written it as a .Net application yet, because that requires that people browse for the file inside the .NET application uless I am mistaken.
Is it possible to make an Addin which works both excel and word, or a dll? AnAnother important thing is that it should be easy to roll out a new version to the user, like stored on a network drive or similar.
Yes it is possible
The Hard Way
You can create a .Net DLL and call it from VBA. In visual studio a lot of people use Unmannaged Exports by Robert Giesecke to create DLLs that don't need to be registered (that way the DLL can be shipped with your document, and as long as it can be found you can use it).
Alternatively you might be able to do it manually as shown here by Hans Passant.
The Easy Way
Once the DLL is created you can declare it in a VBA module the same way you declare any other DLL for Late Binding and then call it from your code.
OR if you're happy to create the DLL and add it as a reference (possibly less portable) you can make it COM visible and register it for COM Interop in Visual Studio; this is probably the easiest way to go because you can then use Early Binding.
This is a walk through that might help: http://www.geeksengine.com/article/create-dll.html
But if you want to store the DLL on a network drive, well it might be that you really want to look at doing it the 'hard way', in which case look here: https://stackoverflow.com/a/5934745/3451115 and here: https://msdn.microsoft.com/en-us/library/bb687915.aspx

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

Preserving clipboard on VSTO load

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.

Any easy way to notify user to update powerpoint add-in

is there a way to notify user about a new version of plugin available?
This can be certainly done by writing some custom code to ping webservice, finding status and based on that displaying message, but then i am wondering if there is a predefined way/template available which can be used and achieved.
If you are sharing sample code, I like C#.
I think one of the best ways to achieve this without writing your own custom code is to use ClickOnce (or a ClickOnce alternative) - "ClickOnce applications can be self-updating; they can check for newer versions as they become available and automatically replace any updated files."
ClickOnce can be and is often used in conjunction with Office addins - for example, see http://robindotnet.wordpress.com/2010/07/11/how-do-i-programmatically-find-the-deployed-files-for-a-vsto-add-in/
Also, another question regarding open-source options to Click-Once might be a good idea - clickonce - what is a good open source alternative to clickonce? (DDay.Update)?

Categories

Resources