1 Replies - 14932 Views - Last Post: 11 November 2010 - 01:07 PM Rate Topic: -----

#1 erburrell   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 149
  • Joined: 22-December 09

Getting Current Excel Application or Workbook in C#

Posted 11 November 2010 - 10:33 AM

All,

I am currently develping an Excel Add-in, and have a quick question. I have a form that I need to allow the user to enter a range of cells in. I have added a button next to the range textbox with the intention of allowing the user to click the button, go to the spreadsheet and select the range, then return the range to the textbox. My question is how do I access the current open file? I know htat I can use
Excel.Application oXL;
oXL = new Excel.Application();

To start Excel, then use similar functions to create a new workbook, etc... but how do I get my Excel.Application object pointed to the current open Excel app, and then point my Excel._Workbook object pointed to the current workbook and the sheet object the same?

Thanks in advance,

Ed.

Current Code is:
 public partial class UI_SteamPropForm : Form
    {
        public UI_SteamPropForm()
        {
            InitializeComponent();
        }

        private void UI_SteamPropForm_Load(object sender, EventArgs e)
        {
            
            ui_PressureCheckBox.Checked = true;
            ui_TempCheckBox.Checked = false;
        }
        
        private void ui_PressureCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (ui_PressureCheckBox.Checked)
                ui_TempCheckBox.Checked = false;
        }

        private void ui_TempCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (ui_TempCheckBox.Checked)
                ui_PressureCheckBox.Checked = false;
        }

        private void ui_CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ui_SelectCellsButton1_Click(object sender, EventArgs e)
        {
            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            Excel.Range oRng;


        }
    }


notice that this is a form called when someone hits a button on the ribbon menu, and thus is not in the ThisAddIn.cs file.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting Current Excel Application or Workbook in C#

#2 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Getting Current Excel Application or Workbook in C#

Posted 11 November 2010 - 01:07 PM

According to http://efreedom.com/...-OpenXML-CSharp

you can achieve it with:

Excel._Workbook activeWorkBook = (Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;


Hope this helps you out ^^

PS
If you have any questions regarding C# and Excel, you can ask me [or at this forum :P]. I am quite into Excel automation via C#, though, I am still a beginner. ^^

This post has been edited by karabasf: 11 November 2010 - 01:08 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1