We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to declare active workbook in Iworkbook.

Hello,

how to create object of active workbook. just like below  in excel intertop.

Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;

Regards,
Nandkumar Satpute

7 Replies

SA Sivaneswaran Amsu Syncfusion Team April 29, 2019 10:50 AM UTC

Hi Nandkumar, 

Thank you for contacting Synfusion support. 

We suggest you to use the below code snippet to access an active workbook. 

Code snippet: 
****************** 
IWorkbook workbook = application.ActiveWorkbook; 
 
Please make use of a below code snippet to activate a workbook. 

Code snippet: 
************** 
workbook.Activate(); 

To know more about XlsIO, please refer the below UG documentation link. 

  
Regards, 
Sivaneswaran . A


NA Nandkumar June 13, 2019 11:29 AM UTC

Sorry for the late response. 

I am creating excel add-in and tried this, but application.ActiveWorkbook showing null

Below is the piece of code

    public partial class frmImportWorksheets : Form
    {        
        IApplication application;        
        IWorkbook _wb;
        public frmImportWorksheets()
        {
            InitializeComponent();
            InitializeActiveWorkbook();
        }
        private void InitializeActiveWorkbook()
        {
            try
            {
                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    application = excelEngine.Excel;
                    _wb = application.ActiveWorkbook;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }          
            
        }
    }

please see attached image.




Regards,
Nandkumar Satpute


AV Abirami Varadharajan Syncfusion Team June 14, 2019 01:41 PM UTC

Hi Nandkumar, 

‘Activeworkbook’ property is returning null as the worbook is neither created nor loaded after initializing the ExcelEngine. So, we request you to create a workbook before retrieving ActiveWorkbook.  Please refer to below code to achieve the same. 

Code Example: 
            using (ExcelEngine excelEngine = new ExcelEngine()) 
            { 
                application = excelEngine.Excel; 
                IWorkbook workbook = application.Workbooks.Create(1); 
                _wb = application.ActiveWorkbook; 
            } 
 
Kindly try this and let us know if this works fine at your end. 

Regards, 
Abirami 



NA Nandkumar June 15, 2019 12:43 AM UTC

Hi Abirami,

I have tried this but it is not taking active workbook copy, it is creating new instance.

 using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    application = excelEngine.Excel;
                    IWorkbook workbook = application.Workbooks.Create(1);
                    _wb = application.ActiveWorkbook;
                    _wb.Worksheets[0].Range["A1"].Value = "Testing"; // this does not set value to active workbook/worksheet cell.
                }

Regards,
Nandkumar Satpute


AV Abirami Varadharajan Syncfusion Team June 17, 2019 12:40 PM UTC

Hi Nandkumar, 

We are unable to reproduce the issue. Also, the value is set to the active workbook `_wb`. You can verify this by checking the value using below code.  

string value = _wb.Worksheets[0].Range["A1"].Value; 

You can also save the active workbook assigned to `_wb` to see if the value is set to the cell `A1`. If you still face issue, please share the issue reproducing sample which will be helpful for us to validate further from our side. 

Regards, 
Abirami 



NA Nandkumar June 18, 2019 04:32 AM UTC

Hello Abrami,

Please check this image.

private void InitializeActiveWorkbook()

        {

            try

            {

                using (ExcelEngine excelEngine = new ExcelEngine())

                {

                    application = excelEngine.Excel;

                    IWorkbook workbook = application.Workbooks.Create(1);

                    _wb = application.ActiveWorkbook;

                    _wb.Worksheets[0].Range["A1"].Value = "Testing"; // this does not set value to active workbook/worksheet cell.

                    string str = _wb.Worksheets[0].Range["A1"].Value = "Testing";

                    _wb.Worksheets.Create("TestWorksheetCreated");

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }



It is assigning the value to object but is not reflecting in the active workbook. You can try creating new excel add-in.

Regards,
Nandkumar Satpute


AV Abirami Varadharajan Syncfusion Team June 19, 2019 10:11 AM UTC

Hi Nandkumar, 

From the shared details, we found that you are using Excel add-in project in which you can’t assign the XlsIO’s workbook to the Excel workbook. You can refer to below article for working with Micorsoft Excel add-in. 


Regards, 
Abirami 


Loader.
Live Chat Icon For mobile
Up arrow icon