locked
Call macro from C# RRS feed

  • Question

  • Hello:

    I have an Excel Workbook Project withs VSTO and what i need is to run an existing vb macro.

    I create the macro with Visual Studio Macros (Alt-F11) inside VS 2005 and the look like this:

    Imports System

    Imports EnvDTE

    Imports EnvDTE80

    Imports System.Diagnostics

    Public Module Module1

    Public Sub Test()

    MsgBox("Hola amigos")

    End Sub

    End Module

    Then i want to excute this macro from a sheet of my project, the look look like this:

    using System;

    using System.Data;

    using System.Drawing;

    using System.Windows.Forms;

    using Microsoft.VisualStudio.Tools.Applications.Runtime;

    using Excel = Microsoft.Office.Interop.Excel;

    using Office = Microsoft.Office.Core;

    using System.Reflection;

    namespace ExcelWorkbook3

    {

    public partial class Sheet1

    {

    private void Sheet1_Startup(object sender, System.EventArgs e)

    {

    this.Application.Run("RegisterCallback",missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);

    }

    private void Sheet1_Shutdown(object sender, System.EventArgs e)

    {

    }

    #region VSTO Designer generated code

     

    When i execute the application VS send this error:

    The macro 'RegisterCallback' cannot be found.


    ************** Exception Text **************
    System.Runtime.InteropServices.COMException (0x800A03EC): The macro 'RegisterCallback' cannot be found.

    Server stack trace:


    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at Microsoft.Office.Interop.Excel._Application.Run(Object Macro, Object Arg1, Object Arg2, Object Arg3, Object Arg4, Object Arg5, Object Arg6, Object Arg7, Object Arg8, Object Arg9, Object Arg10, Object Arg11, Object Arg12, Object Arg13, Object Arg14, Object Arg15, Object Arg16, Object Arg17, Object Arg18, Object Arg19, Object Arg20, Object Arg21, Object Arg22, Object Arg23, Object Arg24, Object Arg25, Object Arg26, Object Arg27, Object Arg28, Object Arg29, Object Arg30)
       at ExcelWorkbook3.Sheet1.Sheet1_Startup(Object sender, EventArgs e) in C:\Documents and Settings\Willy\My Documents\Visual Studio 2005\Projects\ExcelWorkbook3\ExcelWorkbook3\Sheet1.cs:line 17
       at Microsoft.Office.Tools.Excel.Worksheet.OnStartup()
       at ExcelWorkbook3.Sheet1.FinishInitialization() in C:\Documents and Settings\Willy\My Documents\Visual Studio 2005\Projects\ExcelWorkbook3\ExcelWorkbook3\Sheet1.Designer.cs:line 59
       at Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecutePhase(String methodName)
       at Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecuteCustomizationStartupCode()
       at Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecuteCustomization(IHostServiceProvider serviceProvider)


    ************** Loaded Assemblies **************


    How can i fix this? I appreciate your help

     

    Regards

     

    WSG

    Wednesday, November 15, 2006 10:09 PM

Answers


  • The thing is, and that can be confusing if you are not familiar with the VSTO/VS environment, is that it looks as if you are working from withing Excel. So this could bring up the expectation that if you hit Alt-F11 that you enter the VBA IDE from Excel, but in fact you enter the Macro IDE for Visual Studio.

    It sure is a compliment for the team that embedded the Excel/Word interfaces within the VS IDE. The user obviously has the feeling as if he worked in Excel and not in Visual Studio.

    -= Maarten =-

    Thursday, November 16, 2006 2:27 PM

All replies


  • It looks easy to me, as "RegisterCallback" is really not there. I would say your macro is called "Test".

    - Added -
    Re-reading your question also pointed out to me that you create the macro in the VS IDE? You have to create the macro in the VBA environment for Excel.

    -= Maarten =-

    Wednesday, November 15, 2006 10:17 PM
  • Yeah, your right, the macro is Test and the error is the same.

    I create the macro in Visual Studio 2005, pressin ALT-F11, then i create the module and the sub Test().

     

    Wednesday, November 15, 2006 10:23 PM
  • Let me understand your point, i need to create the macro in Excel VBA environment? This means open excel and create the macro? Outside of my Excel Workbook project?

    What's the difference to create it in my Excel Workbook Project?

     

    Wednesday, November 15, 2006 10:28 PM
  • What you did with the Alt-F11 is create a macro in the Visual Studio IDE. That has nothing to do with your Excel project. The VS IDE macros are used to for example automate some programming tasks. Visual Studio is not a VBA editor, use Excel for that.

    If you want to add an Excel VBA macro you have to add this to your Excel document or template. You can do this in the document or template that is processed by VSTO (rightclick the xls or xlt and select "Open folder on disk") or you can add the macro afterwards in the xlt of xls located in your output directory.

    -= Maarten =-

    Thursday, November 16, 2006 9:56 AM
  • WSG,

    Probably you are looking at writing your code in VS and running it in xl. If that is your requirement then you should not write a VBA macro in VS instead write an xl add-in in VS which would be associated to xl.

    If you are actually looking to write VBA then as Marteen suggests you need to write it in the VBA editor of xl and not VS.

    Regards,
    Pavan

    Thursday, November 16, 2006 1:25 PM

  • The thing is, and that can be confusing if you are not familiar with the VSTO/VS environment, is that it looks as if you are working from withing Excel. So this could bring up the expectation that if you hit Alt-F11 that you enter the VBA IDE from Excel, but in fact you enter the Macro IDE for Visual Studio.

    It sure is a compliment for the team that embedded the Excel/Word interfaces within the VS IDE. The user obviously has the feeling as if he worked in Excel and not in Visual Studio.

    -= Maarten =-

    Thursday, November 16, 2006 2:27 PM
  •  Maarten van Stam wrote:


    The thing is, and that can be confusing if you are not familiar with the VSTO/VS environment, is that it looks as if you are working from withing Excel. So this could bring up the expectation that if you hit Alt-F11 that you enter the VBA IDE from Excel, but in fact you enter the Macro IDE for Visual Studio.

    It sure is a compliment for the team that embedded the Excel/Word interfaces within the VS IDE. The user obviously has the feeling as if he worked in Excel and not in Visual Studio.

    -= Maarten =-

     WSG wrote:

    Thanks a lot Martin, i appreciate your help.

    Thursday, November 16, 2006 4:04 PM
  •  Maarten van Stam wrote:

    It sure is a compliment for the team that embedded the Excel/Word interfaces within the VS IDE. The user obviously has the feeling as if he worked in Excel and not in Visual Studio.

    -= Maarten =-

    No doubt on that! VSTO team has indeed done a great job!

    Thursday, November 16, 2006 5:11 PM