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.
Related
To get the Winodws Startup folder I could use:
textBox1.Text = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
Is there a similar method for getting both the Office Startup folder, and the Word startup folder?
For example. Below are the two example of what I'm looking for:
"C:\Program Files (x86)\Microsoft Office\Office14\STARTUP"
"C:\Users\Jim\AppData\Roaming\Microsoft\Word\STARTUP"
Thanks
You will need to look inside the registry to find that information. Have a look at this page for more information. It will show you the places to look and give you examples in Visual Basic.
I had this problem when installing a .dotm file for a Word addin. The best way I found was to create a Word App object and query it for the startup path. This gets you the same folder you would get from VBA in Word using Application.StartupPath. Once you have the startup path you need to close the Word App. This takes some time (like a second) and you need to wait until this is done before proceeding. Here is the install script code to do this:
try
set wordApp = CoCreateObject("Word.Application");
wordStartupPath = wordApp.StartupPath;
// Without delays wordApp.quit sometimes fails
Delay(1);
wordApp.quit;
Delay(2);
set wordApp = NOTHING;
catch
MessageBox("Word Startup Path Cannot be found", INFORMATION);
endcatch;
Here is the same in C#. Here it waits for the process to finish or max 5 seconds. There is probably a better way to do the waiting but this works:
// Get the startup path from Word
Type wordType = Type.GetTypeFromProgID("Word.Application");
object wordInst = Activator.CreateInstance(wordType);
string wordStartupPath = (String)wordType.InvokeMember("StartupPath", BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null,
wordInst, null);
Thread.Sleep(1000);
wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordInst, null);
// Make sure Word has quit or wait 5 seconds
for (int i = 0; i < 50; i++)
{
Process[] processes = Process.GetProcessesByName("winword");
if (processes.Length == 0)
break;
Thread.Sleep(100);
}
At the top of the file you will need
using System.Diagnostics;
I have a simple excel file that has the following code:
Private Sub Workbook_Open()
MsgBox "Hello World!"
End Sub
I tried to run a sql job to open this excel file but it failed, became unresponsive. On googling, I found the reason 'why' SQL job wouldn't open excel file
Job On Sql Server Agent does not complete, but it does in BIDS?
So I thought of creating a simple console application in C# which would simply open the excel file and run my macro. Here's my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;
namespace T_OpenExcel
{
class Program
{
static void Main(string[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
//Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(#"E:\data_extracts\TestHelloWorld.xlsm", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
RunMacro(xlWorkBook, new Object[] { "TestHello" });
Thread.Sleep(5000);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
}
private static void RunMacro(Excel.Workbook xlWorkBook, object[] p)
{
//throw new NotImplementedException();
}
}
}
I could successfully run this code in IDE. Now I want to run this from SQL job in SSMS, sql server 2008.
I grabbed T_OpenExcel.exe file from my C# project( T_OpenExcel-->bin-->Debug-->T_OpenExcel.exe). I created a SQL Job.Here are some of my details:
Step name: OpenHelloWorldExcel
Type:Operating system(CmdExec)
Run as: SQL Server Agent Service Account
Command: C:\Users\shress2\Documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\bin\Debug\T_OpenExcel.exe
On running this job, I get the following status
Start Job 'TestHelloWorld' Status Success
Execute job 'TestHelloWorld' Status Error
On viewing history, it shows:
Message
Executed as user: GSOPS4\SYSTEM. Unhandled Exception: System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 'E:\data_extracts\TestHelloWorld.xlsm'.
There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook. at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad) at T_OpenExcel.Program.Main(String[] args) in C:\Users\shress2\documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\Program.cs:line 23. Process Exit Code -532462766. The step failed.
I checked E:\data_extracts\TestHelloWorld.xlsm directory and found it working. I made sure my xlsm file is not being used by anyone. I couldn't figure out why its failing to run it. Any help is greatly appreciated. Thanks in adv!
The problem appears to be that you're attempting to show a messagebox while opening the Excel file, but you're automating it, so there's no human to click the button on the messagebox. The rest of the code can't execute because the file is sitting there waiting for someone to click the button.
On subsequent runs, the server can't access it because it's still sitting there, invisible to normal users, waiting for the button to be clicked. hence the errors.
Short version: Don't proompt for user input on an app that will be run unattended.
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.
I wrote a VSTO Excel addin using Visual Studio 2010 and after having managed to work around most of the obstacles Microsoft throws into the path of the righteous developer, I finally have to admit defeat.
My project contains a Ribbon with some controls, a custom task pane that allows users to search a database via a RESTful interface and a RTD server that lets them put this data in their worksheets. So far, so ... well, painful, I guess: After a lot of struggle with Interop, ComVisibility and AppDomains (what a great idea!), my current status is as follows.
In the worksheets, I call a wrapper function for RTD like this (snipped):
Public Function call(value as String)
Dim addin as Office.ComAddIn
Set addin = Application.ComAddIns("MyAddin")
addin.Object.RTD(value)
End Function
This is (part of) the addin class:
namespace Some
{
[Guid("...")]
[ComVisibleAttribute(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyAddin {
[snip]
public String RTD(String value)
{
String returner = null;
try
{
returner = Globals.ThisAddin.Application.WorksheetFunction.RTD(SERVERID, "", value);
}
catch(COMException ce)
{
returner = ce.StackTrace;
}
return returner;
}
}
}
And the relevant part of the RTD Server class:
namespace Some
{
[Guid("...")]
[ComVisibleAttribute(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("MyRTDServer")]
public class Server : Excel.IRtdServer
{
[snip]
}
}
In Debug mode I:
Create a, empty new workbook
Add an "=RTD(...)" formula to a cell
Add a wrapper function call "=call(...)" to a cell
Save the workbook
Open the workbook
Stop Debugging and start it again
Open the workbook
I observe:
At 3, everything works fine
At 5, everything works fine
At 7, when recalculating my cells, I get a Unable to get the RTD property of the WorksheetFunction class exception in the cell of 3) and #N/A in the cell of 2). However, I can see that the topics are registered in the RTD server and as soon as the data is available, the exception is replaced with the correct data. Also, if I do NOT recalculate the cells, they display the saved value and then correctly update to the retrieved value once the data is available.
If in deployed mode, I observe:
At 2, I get #N/A
At 3, I get a Unable to get the RTD property of the WorksheetFunction class exception
Any help, please? :(
EDIT:
Testing the same procedure with a very basic RTD server in a blank Addin project shows exactly the same results: a loaded excel file displays #N/A if an RTD-formula is recalculated before the server has the data available.
I would like to inquire: WTF?
Cheers,
Che
The 'Unable to get the RTD property....' error is just the Excel object model's way of saying there was an error in the function call.
Does your RTD server have any dependencies on the rest of your add-in? The fact that 2) fails when deployed, makes it look like the RTD server needs some other bits to be initialized before it runs happily. The way the VSTO add-in is put together, your assembly is likely to be loaded totally separately for the RTD server and for the COM add-in, and in your problematic cases the RTD instance is loaded first.
Perhaps you can test with a sample RTD server instead of your own, just to confirm that the error is there and not in the wrapper or elsewhere in the VSTO add-in. Then you can dig into your RTD server to see what goes wrong.
-Govert
Excel-DNA - Free and easy .NET for Excel
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.