Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C#.NET 3.5 Excel 2007 Ribbon Add-in VS 2008 run macro

C#.NET 3.5 Excel 2007 Ribbon Add-in VS 2008 run macro

Scheduled Pinned Locked Moved C#
csharpvisual-studioasp-netlinqcom
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    Good morning. Using Visual Studio 2008 (C# - Excel 2007 Add-in) I created a Excel 2007 Ribbon tab named Macros. It contains several buttons. I got some sample code online, but I can seem to run the macros. I have the following:

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using System.Reflection;
    using Excel = Microsoft.Office.Interop.Excel;
    using Microsoft.Office.Core;

    private void btnImport_Click(object sender, RibbonControlEventArgs e)
    {

    // Object for missing (or optional) arguments.
    object oMissing = System.Reflection.Missing.Value;

    // Create an instance of Microsoft Excel, make it visible,
    // and open Book1.xls.
    Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
    oExcel.Visible = true;
    Excel.Workbooks oBooks = oExcel.Workbooks;
    Excel._Workbook oBook = null;
    oBook = oBooks.Open("c:\\Book1.xlsx", oMissing, oMissing,
    oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
    oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

    // Run the macros.
    RunMacro(oExcel, new Object[] { "Import" });
    //RunMacro(oExcel, new Object[]{"DoKbTestWithParameter",

    }
    private void RunMacro(object oApp, object[] oRunArgs)
    {
    oApp.GetType().InvokeMember("Run",
    System.Reflection.BindingFlags.Default |
    System.Reflection.BindingFlags.InvokeMethod,
    null, oApp, oRunArgs);
    }

    Any idea what might be an issue with the code? WHEELS

    W 1 Reply Last reply
    0
    • W Wheels012

      Good morning. Using Visual Studio 2008 (C# - Excel 2007 Add-in) I created a Excel 2007 Ribbon tab named Macros. It contains several buttons. I got some sample code online, but I can seem to run the macros. I have the following:

      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using Microsoft.Office.Tools.Ribbon;
      using System.Reflection;
      using Excel = Microsoft.Office.Interop.Excel;
      using Microsoft.Office.Core;

      private void btnImport_Click(object sender, RibbonControlEventArgs e)
      {

      // Object for missing (or optional) arguments.
      object oMissing = System.Reflection.Missing.Value;

      // Create an instance of Microsoft Excel, make it visible,
      // and open Book1.xls.
      Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
      oExcel.Visible = true;
      Excel.Workbooks oBooks = oExcel.Workbooks;
      Excel._Workbook oBook = null;
      oBook = oBooks.Open("c:\\Book1.xlsx", oMissing, oMissing,
      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

      // Run the macros.
      RunMacro(oExcel, new Object[] { "Import" });
      //RunMacro(oExcel, new Object[]{"DoKbTestWithParameter",

      }
      private void RunMacro(object oApp, object[] oRunArgs)
      {
      oApp.GetType().InvokeMember("Run",
      System.Reflection.BindingFlags.Default |
      System.Reflection.BindingFlags.InvokeMethod,
      null, oApp, oRunArgs);
      }

      Any idea what might be an issue with the code? WHEELS

      W Offline
      W Offline
      Wheels012
      wrote on last edited by
      #2

      Update - I have the following:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using Microsoft.Office.Tools.Ribbon;
      using System.Reflection;
      using Excel = Microsoft.Office.Interop.Excel;
      using Microsoft.Office.Core;
      using System.Windows.Forms;

      namespace ExcelAddInCustomTab
      {
      public partial class RibbonCustom : OfficeRibbon
      {
      public RibbonCustom()
      {
      InitializeComponent();
      }

      private void btnFileImport_Click(object sender, RibbonControlEventArgs e)
      {
      Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
      RunMacro(oExcel, new Object[] { "FileImport" });
      }

      private void RunMacro(object oApp, object[] oRunArgs)
      {
      try
      {
      oApp.GetType().InvokeMember("Run",
      System.Reflection.BindingFlags.Default |
      System.Reflection.BindingFlags.InvokeMethod,
      null, oApp, oRunArgs);
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.InnerException.Message);
      }
      }

      }
      }

      In the Excel 2007 spreadsheet, the Import tab shows up with the several buttons I set up. I am getting the following error: The macro may not be available in this workbook, or all macros may be disabled. I checked all the trusted location and permission settings, and the add-in and macros are not disabled. WHEELS

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups