Solved

cannot call macro rtn in Excel within .NET application

Posted on 2011-09-22
3
258 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
  • 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 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now