I've build an excel addin which fills a worksheet with data from a database. I also add some styling and lock some rows and columns by using FreezePanes.
worksheet.Activate();
worksheet.Application.ActiveWindow.FreezePanes = false;
worksheet.Application.ActiveWindow.SplitRow = 4;
worksheet.Application.ActiveWindow.SplitColumn = 11;
worksheet.Application.ActiveWindow.FreezePanes = true;
This all worked like a charm in excel 2010/2013 but I recently switched to excel 2016 (office 365) and from then on I had problems with the FreezePanes when my excel worksheet is not on the foreground. I searched the internet and the only thing I come across is that I can only preform a FreezePanes on an active sheet, I knew that - I allready do activate the sheet before setting the FreezePanes. This worked in excel 2010, even though physically my excel wasn't sent to the foreground.
Excel from the office 365 probably really want my excel worksheet to be physically in the foreground but worksheet.Activate()
doesn't help and I also tried the following code:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
string caption = oExcel.Caption;
IntPtr handler = FindWindow(null, caption);
SetForegroundWindow(handler);
But this too didn't work. Can any body help me with this one?
To be clear: The version of my excel is 2016 Version 1611 (Build 7571.2109)
worksheet
in the first block? – Han SoaloneExcel.Application.ActiveSheet
During the process it can be that this WorkSheet is no longer the active one. For some strange reason the sheet is more or less active when I activate it, but it is not send to the foreground and FreezePanes fails... – DeniseMeander