简体   繁体   中英

How to Open a newly Created Excel WOrkBook as Active Workbook

I have developed an Addin in Excel. This addin has a ribbon that contains a button. When this button is clicked, I create a new work book and work sheet based on an xml string that is retrived from the database based on the user credential. Iam able to create the workbook along with the work sheets, however, this work book is not displayed as the primary work book for the user.

What I would like to acheive is : When the addin is clicked on an excel work book , the generated work book shall become the active one and the old excel work book goes visually so that the users attention is now focussed on the created work sheet instead of him having to search for the created work book.

Here is the code that I have used, but it does not seem to work : Ps : Iam using Excel 2013 and VS 2013 and C#

var excel = Globals.ThisAddIn.Application; 
Microsoft.Office.Interop.Excel.Workbook newWorkBook = null; 
newWorkBook = Globals.ThisAddIn.Application.Workbooks.Add(); 
#region BringGeneratedWorkBookToFront 
string newWorkBookName = newWorkBook.Name; 
foreach (Workbook wb in excel.Workbooks){ 
   if (wb.Name == newWorkBookName) { 
      wb.Activate(); 
      break; 
   } 
}
#endregion 

Here is your code simplified:

Excel.Workbook newWorkBook =Globals.ThisAddIn.Application.Workbooks.Add();
newWorkBook.Activate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM