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. :(
Related
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
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.
I am developing an application that will receive data through COM port and needs to paste it into a Microsoft Office program (for now, I'm working with Word, but it will support Excel, PowerPoint, ...).
I thought that a nice idea would be copying this data to clipboard, then paste it to whatever application window is open (so, for instance, LibreOffice apps and Notepad work perfectly). For this, I've coded:
if (booleanIncomingData)
{
booleanCopy = true;
}
else if (booleanOutcomingData)
{
copy = false;
}
if (copy)
{
Clipboard.SetText(myString);
}
else
{
SendKeys.Send("^V");
SendKeys.Send("{ENTER}");
}
As I said, LibreOffice programs and Notepad work perfectly, but Word and Excel aren't doing what I thought they should (basically display any data, then jump to the next line).
Also, a simple
Clipboard.GetText()ç
doesn't work (and my feeling is because it tries to paste the content into the active window.
Looking for Interop.Word solutions over the web, had no success so far. Does anyone have an idea what can I do or where I should look for? Instead of a regular Win32 application, should I implement an Office add-on?
In case any other information is necessary, please feel free to ask for it.
César.
It got much more complex than I thought. But my current application now works with several softwares (Word, Excel, Notepad, LibreOffice, ...).
COPY/PASTE DATA
stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp
msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
GET THE ACTIVE WINDOW
www.csharphelp.com/2006/08/get-current-window-handle-and-caption-with-windows-api-in-c/
stackoverflow.com/questions/893669/determine-whether-program-is-the-active-window-in-net
stackoverflow.com/questions/2635404/how-to-get-process-name-and-title-of-the-top-window-on-windows-c-sharp
MICROSOFT OFFICE
hubpages.com/hub/How-To-Program-with-Excel-and-C-using-a-Ribbon
msdn.microsoft.com/en-us/library/vstudio/6b9478cs.aspx
stackoverflow.com/questions/13403504/send-text-to-excel-sheet-using-winapi
stackoverflow.com/questions/2378206/how-to-get-current-or-focussed-cell-value-in-excel-worksheet-using-c-sharp
All in all, Microsoft Office applications were more cumbersome, and as #Brian suggested, PIA (Primary Interop Assemblies) were crucial in this task.
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
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.