none
Debuging Excel add-in starts new instance? RRS feed

  • Question

  • I’m trying to make a simple Excel add-in with a ribbon and a button on the ribbon. In the ribbons clicked event I’m trying to get the activeworkbook and activeworksheet. My code gets that when I load the add-in from Excel and click the button on my ribbon but if I try to start debugging F5 when I click my button it starts a new instance of Excel and in the new instance of Excel there are no open or activeworkbooks and my code then gets a null for the activeworkbook. How can I debug the mybutton_clickEvent  without starting the new instance of Excel?


    why767

    Wednesday, November 7, 2012 2:24 PM

Answers

  • GetActiveObject is the reason things are behaving "strangely". 

    If you want to reference something in the Excel application, you do something more like this:

      Excel.Workbook oWB = (Excel.Workbook) Globals.ThisAddin.Application.ActiveWorkbook;
      Excel.Worksheet oSheet = (Excel.Worksheet) oWb.ActiveSheet

    ThisWorkbook only works for document-level customizations, but not for application-level add-ins.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by why767 Friday, November 9, 2012 2:07 PM
    Friday, November 9, 2012 9:23 AM
    Moderator

All replies

  • Hi why767

    You first need to unload any currently loaded version of the Add-in in Excel. If you don't have a "Com Add-ins" button in your Ribbon anywhere you have to go over File/Options/Add-ins, select Com Add-ins from the "Manage" dropdown, then click Go. Remove the checkmark next to the entry for your Add-in.

    Return to Visual Studio and Build/Rebuild the solution.

    Go back to Excel and re-activate the checkmark for your add-in in the COM Add-ins dialog box - this will load the new version of your add-in.

    Back in Visual Studio: Tools/Attach to process and select Excel.exe


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, November 7, 2012 4:35 PM
    Moderator
  • Hi why767,

    Is a new instance opening when you press F5 or is a new instance opening because the code in your handler is creating a new instance of Excel? Just curious. If you are describing the instance that is opened when you press F5, an Excel 2010 workbook is opened for you. In Excel 2013, the Ribbon is not visible until you open a workbook. It seems that in either version of Excel, there would be an active workbook and the only situation where the Ribbon would not be visible is if you closed the workbook.


    Norm Estabrook

    Wednesday, November 7, 2012 7:02 PM
    Answerer
  • Hi Norm

    In my experience, a new instance of the Office application is always started when you press F5 (debug).

    And with Excel, it often happens that no workbook is opened - you have to open one manually or the add-in code has to do so. I have a number of add-ins that, if they're activated (allowed to load) Excel starts with no new workbook.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 8, 2012 6:59 AM
    Moderator
  • Thanks Cindy but I've already tried that.


    why767

    Thursday, November 8, 2012 1:35 PM
  • When I press F5 and the debugger starts a new instance of Excel 2010 starts then I go to the COM Add-ins dialog box to check my add-in to load it. I then open the workbook and select the worksheet I want the add-in to use. I then click the button on the ribbon that I created in my add-in. The add-in code is very simple and does not have anything to start a new instance.

            private void Button1_Clicked(object sender, RibbonControlEventArgs e)
            {
                Excel.Application oXL;
                Excel.Workbook oWB;
                Excel.Worksheet oSheet;
                try
                {
                    oXL = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
                    oXL.Visible = true;
                    oWB = (Excel.Workbook)oXL.ActiveWorkbook;
                    if (oWB != null)
                    {
                        oSheet = (Excel.Worksheet)oWB.ActiveSheet;
                    }
                        else { return; }
                }
                catch
                {
                }
    
            }
    


    why767

    Thursday, November 8, 2012 1:48 PM
  • The new instance only starts when I click the ribbon button and I have a workbook open and only when I'm debuging. If I just load the add-in from Excel and click the button it works fine.

    why767

    Thursday, November 8, 2012 1:51 PM
  • Hmm. A new instance of Excel shouldn't start when you click the Ribbon button unless your handler code is causing that to happen. If you are trying to access the active worksheet from Ribbon button handler code, why not just use the following?

            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Excel.Worksheet oSheet = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;
            }
    Norm E.


    Norm Estabrook

    Thursday, November 8, 2012 6:32 PM
    Answerer
  • When I use;

    Excel.Worksheet oSheet = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;

    I get;

    .Globals' does not contain a definition for 'ThisWorkbook'


    why767

    Thursday, November 8, 2012 9:51 PM
  • Hmm. Strange. Is this a VSTO add-in or some other type of add-in (such as COM or Shared). If you added a Ribbon project item to an VSTO Excel add-in, then 'ThisWorkbook' should be available on the Globals class.

    Norm Estabrook

    Thursday, November 8, 2012 10:07 PM
    Answerer
  • I'm using VS2010 Academic. I created my project from the Visual C# > Office 2010 "Excel 2010 Add-In" Template. Then I just right clicked the project in the Solution Explorer and selected add > New Item. From the add new item dialog under Visual C# Items I selected the Ribbon (Visual Designer). In the designer I add a button from the tools menu then doubleclicked it and entered the code posted in the event handler.

    why767

    Thursday, November 8, 2012 10:27 PM
  • GetActiveObject is the reason things are behaving "strangely". 

    If you want to reference something in the Excel application, you do something more like this:

      Excel.Workbook oWB = (Excel.Workbook) Globals.ThisAddin.Application.ActiveWorkbook;
      Excel.Worksheet oSheet = (Excel.Worksheet) oWb.ActiveSheet

    ThisWorkbook only works for document-level customizations, but not for application-level add-ins.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by why767 Friday, November 9, 2012 2:07 PM
    Friday, November 9, 2012 9:23 AM
    Moderator
  • Yes that was the problem. I didn't know the correct way to get the ActiveWorkbook. Thanks

    why767

    Friday, November 9, 2012 2:07 PM
  • Thanks for your time and effort

    why767

    Friday, November 9, 2012 2:09 PM