Solved

cannot call macro rtn in Excel within .NET application

Posted on 2011-09-22
3
263 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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 …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

732 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