What is the best way to bring a newly opened workbook to the front?

Add-in Express™ Support Service
That's what is more important than anything else

What is the best way to bring a newly opened workbook to the front?
 
Anthony Pitts




Posts: 4
Joined: 2016-08-26
Hi,

I am opening a workbook using the following code:


var workbooks = excelApp.Workbooks;
var workbook = workbooks.Open(tempFile);

Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(workbook);

workbooks = null;
workbook = null;


...but the newly opened workbook is behind the original window. I've tried several methods, such as workbook.Activate(), but none seem to work.


EDIT:
The following code does what I want, but seems like there should be a better way:

var windowState = excelApp.Application.ActiveWindow.WindowState;
excelApp.Application.ActiveWindow.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
var workbooks = excelApp.Workbooks;
var workbook = workbooks.Open(tempFile);

excelApp.Application.ActiveWindow.WindowState = windowState;
                
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(workbook);

workbooks = null;
workbook = null;


Is there a more appropriate way to bring the newly opened workbook to the foreground?

Anthony
Posted 15 Mar, 2017 19:00:17 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Anthony,

Try workbook.Activate().


Andrei Smolin
Add-in Express Team Leader
Posted 16 Mar, 2017 05:27:09 Top
Anthony Pitts




Posts: 4
Joined: 2016-08-26
Hi Andrei,

I've tried workbook.Activate() but it has no effect. I'm using Office 2016 if that helps.

Anthony
Posted 22 Mar, 2017 20:32:25 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Anthony,

I have just tried to test your code in Excel 2016 32-bit. It works with Activate(), an odd thing is that it also works without Activate() - in my case the opened workbook is in foreground.

I used the ribbon button to execute my test code. And you?
Posted 23 Mar, 2017 04:53:31 Top