Background: I have an extensive set of specialized VBA macros used in Word for document formatting purposes. In Word 2003, these macros were activated from a customized toolbar. I have recently transitioned to Word 2007 and would like to be able to run these existing VBA macros from a new Word Ribbon created with VS 2010. I have created a Ribbon; however, I cannot figure out how to call the existing macros from the new Ribbon buttons.
Question: How do I call the existing VBA macros, which are stored in a .dotm template, from the C# Word Add-in?
Any help would be greatly appreciated.
The technique described in MS KB article 306683 -- in particular, function RunMacro defined there -- should allow you to call a VBA macro from within C# code: You define a function RunMacro
private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}
and then call your macro like this:
RunMacro(oApp, new object[] {"NameOfMyMacro"})
or
RunMacro(oApp, new object[] {"NameOfMyMacro", "some", 3, "parameters"})
oApp is the Word.Application object, which I'm sure is available somewhere in a Word add-in.
Related
I am tring to call with =CALLTHIS("pp",) and with =CALLTHIS("ThisDocument.pp",) from shape event dblClick. I´m using C# Visio 2010 Add-in in Visual Studio 2015 without success.
The method is:
public static void pp(Visio.Shape shpObj) {
MessageBox.Show("My id is: " + shpObj.ID);
}
To call a managed C# (VSTO/COM) add-in from shape sheet formula cell, such as "dblClick" event, use QUEUEMARKEREVENT function. CALLTHIS only works for VBA functions. RUNADDON/RUNADDONWARGS only works for unmanaged (VSL) addons.
Here you can find a step-by-step guide how to make it happen:
https://blogs.msdn.microsoft.com/chcast/2004/11/03/calling-com-add-ins-from-the-shapesheet/
With Visual Studio 2013 I target the .NET Framework 4, and created a single add-in that targets both Office 2007 and Office 2010. I choose a 2010 Addin but it should work in 2007 accordind to this link:
https://blogs.msdn.microsoft.com/vsto/2010/06/04/creating-an-add-in-for-office-2007-and-office-2010-that-lights-up-on-office-2010-mclean-schofield/
I used the ribbon designer (not the ribbon xml) In my AddInin the startup method I have the following:
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
((Word.ApplicationEvents4_Event)this.Application).NewDocument += new Microsoft.Office.Interop.Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
this.Application.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);
//THIS LINE FAILS IN Word 2007 but not in Word 2010
this.Application.ActiveDocument.Saved = false;
}
Basically I'm capturing the close event and I do some custom code of my own in that event. This works perfectly in 2010. In 2007 the ribbon installs and some functionality works however when I close the document my 'Application_DocumentBeforeClose' close event dosnt get called in Word 2007. Any suggestions?
UPDATE: I changed my code as suggested however the line below fails in Word 2007 when I open a document with error - 'This command is not available because no document is open.'
this.Application.ActiveDocument.Saved = false;
Take a look at the similar forum thread - Word DocumentBeforeClose not firing. Here is what it states:
If we open a document, Word will detect whether the new document is modifed. If it is the blank document from scratch, Word uses it to host the target document directly. So in this case, it is not considered a Close action. Hence, the DocumentBeforeClose does not fire.
I have a word document, downloaded from the internet, that I would like to perform some operations on in an Application-level Add-In. These operations (document search, unprotecting the document, etc) require that the document be in edit mode when it is opened. Here is some sample code that illustrates my needs:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
Application.DocumentOpen += application_DocumentOpen;
}
private void application_DocumentOpen(Document doc)
{
if (doc.ProtectionType != WdProtectionType.wdNoProtection)
{
// this throws a COMException if the document is opened in read-only mode
doc.Unprotect("password");
}
// ...
}
Since this add-in will be distributed to multiple users, I can't assume that the user will set any application properties, such as opening downloaded documents in edit mode by default, so doing it in code would be ideal. Is there some means with VSTO or the interop library to accomplish this, given my constraints? Thanks for any help.
EDIT: My application-level add-in was tested as installed on Word 2013, and was created with VS 2013, VSTO 4.0.
A sample document that displays the requisite characteristics can be found here. The document is protected with WdProtectionType.wdAllowOnlyFormFields, and the password is "password".
What version of Word/VSTO are you using.
I tried this using Word 2013 ( 64-bit ) and VSTO 4.0/Visual Studio 2013 and do not get any Exception in the even handler function "application_DocumentOpen". For both read-only and protected docs.
EDIT:
Try to change the view to Print View before unprotecting the document.
if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection)
{
doc.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
doc.Unprotect("password");
}
Reference : http://answers.microsoft.com/en-us/office/forum/office_2013_release-word/not-available-for-reading-error-on-unprotecting-a/a888701b-d70a-4dbc-a1ec-68b8bad80848
VSTO
VS2008 SP1
.NET 3.5
Excel 2007
I am a .net noob. I am trying to load an automation addin that is an excel application/automation addin (it is a dll not xla or xll) from within a vsto addin in the ThisAddIn_Startup() method of the vsto addin. From google I got the below solution which is not working.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application excel = Globals.ThisAddIn.Application;
//Also tried without display alerts being set to false
excel.DisplayAlerts = false;
foreach (AddIn addin in excel.AddIns)
{
if (addin.progID.Equals("MY_ADDIN_PROG_ID"))
{
Debug.WriteLine("Addin installed is " + addin.Installed);
addin.Installed = false;
Debug.WriteLine("Addin is: " + addin.FullName + ", " + addin.progID);
Debug.WriteLine("Addin installed is " + addin.Installed);
}
}
AddIn addIn = excel.AddIns.Add("MY_ADDIN_PROG_ID", false);
addIn.Installed = true;
excel.DisplayAlerts = true;
Debug.WriteLine("Addin is: " + addIn.FullName + ", " + addIn.progID);
Debug.WriteLine("Addin installed is " + addIn.Installed);
excel.DisplayAlerts = false;
//OTHER STARTUP CODE
Debug.WriteLine("Starting up addin!");
}
Note, I can see the addin.installed is being set to false and back to true on startup but when I try to populate worksheet with udfs from the addin I tried to load in a later button_click method, I get #NAME? error. I am at my wits end. Any help will be greatly appreciated.
If I first try to call the udf in excel by typing it in a cell by hand before I call my button click method, the worksheet population works and the udfs get evaluted as expected but this is not ideal.
Also setting installed property to true does not seem to be doing anything as i can still see the udf addin as inactive in excel, it is only if I type it into a cell that it gets activated. Is there anything else I need to do to activate the automation addin in my vsto startup?
Thanks!
I'm not sure you want to do this in the startup event. I have done something similar but not quite the same before which may be applicable. I exposed some COM visible functions to VBA in a different event handler:
protected override object RequestComAddInAutomationService()
{
// return something com-visible
}
So maybe you can try to load your automation dll this way? This happens before the startup event fires... Excel might be doing something like locking its list of addins while a startup event is being handled - who knows? If it were possible to know Excel programming would be less tedious.
It is harder than it seems to combine VSTO and Automation in Excel. You may find my blog post helpful:
Communicating Between VSTO and UDF's in Excel
Just need to add String Value to the following registry key and you are good.
For Office 2007
Find regkey, HKEY_CURRENT_USER\SOftware\Microsoft\Office\12.0\Excel\Options, then create string value, where name = OPEN, value = /A "YOUR ADDIN NAME HERE" (quotes need to be included as well.)
Note that for the first addin, value name should be called OPEN, for the second one and onwards, use OPEN1, OPEN2, ... etc.
For Office 2010
Just replace 12.0 with 14.0 in the above regkey path, the rest are all the same.
Check out below article on MSDN, which will also help you a lot.
http://support.microsoft.com/kb/291392
Looks like this is a bug specific to VSTO. I converted my addin to a COM addin and was able to use the automation addin from code after that. My team has sent the issue to microsoft so we'll see what they say.
How to run Excel macros from VS2010(C#)?I use Microsoft.Office.Interop.Excel namespace
Try this article:
http://support.microsoft.com/kb/306683
The relevant part to run a macro is this (where oApp is the application instance in your code):
private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}
In addition to amarsuperstar's answer,
I'd like to state that you program needs to be a trusted source in order to call these macros. And the Excel-File itself needs to be trusted as well.