?
Solved

cannot call macro rtn in Excel within .NET application

Posted on 2011-09-22
3
Medium Priority
?
273 Views
Last Modified: 2013-12-17
I am trying to execute a macro using the opened workbook object in the code
but it cannot find the macro's function.  The macro itself is an add-in.  I suspect when I open the workbook (in code), all the initialization steps have not been
taken since I do not see the excel menubar option with the macro functions listed.  Oddly, when I open an excel instance on my desktop, it does initialize properly and
I do see the menubar pulldown in excel.
 
 
 My code looks like this:
 
app = new Microsoft.Office.Interop.Excel.Application();

app.Visible = true;

try

{

wbk = app.Workbooks.Open(uploadfilewbk, 3, false, 5, "", "", true, XlPlatform.xlWindows, "", true, false, 0, true, false, false);



Worksheet wks = (Worksheet)wbk.Sheets["PositionLink"];

int recindx = 8;

// write new positions form riskpos struct

foreach (string thekey in riskpos.Keys)

{

Dictionary<string, string> therec = riskpos[thekey];

wks.Cells[recindx,1] = therec["Portfolio"];

++recindx;

}


wbk.Save();

}

catch (Exception ee)

{

System.Console.WriteLine("Main Error " + ee.Message);

}

// Trigger Macro

try

{

app.Run("EMPositionLink_AddEquitiesTransactions",

Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

}

catch (Exception eee)

{

System.Console.WriteLine("ERROR: {0}", eee.Message);

}

 Also, I was able to set a break right after the Open call and was able to trigger the initialization after pushing one of the buttons that calls this macro.  I was then able to see it in the visual basic editor within the .xla module it was contained in

0
Comment
Question by:cchu1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:cchu1
ID: 36582532
Please help me with the problem above.  Thanks.
0
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 1000 total points
ID: 36586678
other ways to call a macro:

app.RunOld("EMPositionLink_AddEquitiesTransactions");

app.GetType().InvokeMember("Run"
  , System.Reflection.BindingFlags.Default |
    System.Reflection.BindingFlags.InvokeMethod
  , null
  , app
  , "EMPositionLink_AddEquitiesTransactions"
);
0
 

Author Comment

by:cchu1
ID: 36587147
Thanks, I found my problem.  Needed to state the full path of the routine..  but I can use your suggestion in the future.

app.Run("'C:\\Program Files....\\<file>.xla!EMPos..._AddEquitiesTransactions",Missing.Value,Missing.Value, ...);
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question