"Cannot set the FreezePanes property of the Window class" Excel 2016 (Office 365)

I built an excel addin that populates a worksheet with data from a database. I also add some style and block some rows and columns with FreezePanes.

worksheet.Activate();
worksheet.Application.ActiveWindow.FreezePanes = false;
worksheet.Application.ActiveWindow.SplitRow = 4;
worksheet.Application.ActiveWindow.SplitColumn = 11;
worksheet.Application.ActiveWindow.FreezePanes = true;

It all worked like a charm in excel 2010/2013, but I recently switched to excel 2016 (office 365), and since then I had problems with FreezePanes when my excel worksheet was not in the foreground. I searched the Internet, and the only thing I came across was that I can only harvest FreezePanes on the active sheet, I knew that - I already activated the sheet before installing FreezePanes. This worked in excel 2010, although physically my excel was not brought to the fore.

Office 365 Excel probably really wants my excel worksheet to be physically in the foreground, but it 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 that didn't work either. Can anyone help me with this?

To be clear: my excel version is 2016 version 2016 version (Build 7571.2109)

+4
source share
3 answers

Yes! I fixed this as Xatoo suggested by adding:

Worksheet.Application.ActiveWindow.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;

It is important to add that this solution only works when the window is actually minimized, so you need to check this.

The funny part is that it still does not send the window to the foreground.

0
source

Is it possible to do this in VBA?

Worksheets("Sheet1").Activate 
ActiveWindow.FreezePanes = False
ActiveWindow.SplitRow = 4
ActiveWindow.SplitColumn = 11
ActiveWindow.FreezePanes = True

, , :

Private Sub Worksheet_Activate() 
 [method that makes stuff happen]
End Sub

MSDN , VBA 2013/365, #.

+2

, worksheet.Application.ActiveWindow , ? Excel , Microsoft MDI Excel, , . , .

. Excel 2013: https://msdn.microsoft.com/en-us/library/office/dn251093.aspx

, : , FreezePane:

Worksheet.Application.ActiveWindow.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;

, Excel. - , , , :

https://social.msdn.microsoft.com/Forums/office/en-US/7e6ff1ed-b4c6-4c75-82be-14175f44df55/freezepanes-throws-an-exception-when-excel-is-minimized?forum=exceldev

Microsoft , .

+1

Source: https://habr.com/ru/post/1666894/


All Articles