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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to declare active workbook in Iworkbook.

Thread ID:

Created:

Updated:

Platform:

Replies:

144283 Apr 26,2019 01:03 PM UTC Jun 19,2019 10:11 AM UTC WinForms 7
loading
Tags: XlsIO
Nandkumar
Asked On April 26, 2019 01:05 PM UTC

Hello,

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

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

Regards,
Nandkumar Satpute

Sivaneswaran Amsu [Syncfusion]
Replied On 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

Nandkumar
Replied On 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

Abirami Varadharajan [Syncfusion]
Replied On 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 


Nandkumar
Replied On 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

Abirami Varadharajan [Syncfusion]
Replied On 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 


Nandkumar
Replied On 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

Abirami Varadharajan [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon