Never underestimate the power of Passion!

Wednesday, 22 March 2017

On 06:01 by Vardan Kumar in ,    9 comments

Unable to set the Freeze Panes property of Window Class C#

It is generally easy to resolve the compile time errors because the reason for the error is probably known but a run time error always hinders the development process for a considerable amount of time so are the exceptions that unknowingly show up from no where. That is why it is always advised to handle each and every exception your application might throw. The only way to be a master in handling exception is experience, yeah you read it right. More you develop, more problem you will encounter in your developing environment and in turn more you will know the reason and the so called timing of the exceptions.

Exception

One of the most encountered is "unable to set freeze panes property of window class"  which show up when you try to freeze panes while formatting an excel file in c#.

Reason

The main reason for occurrence of such an exception is incompatibility between older and the newer versions of MS Excel.

When it occirs

This exception occurs when you are using a newer version of MS Excel and your application gets activate in background, I repeat your MS Excel application thread opens in the minimized state which hinders the freezing pane process.

How to counter this exception?

Obviously to get a solution to a problem, first of all understanding the problem is must more precisely the reason of problem should be known. Since we know the reason why freeze panes property is not set and the exception is thrown we can provide a solution.

So if somehow we can make our MS Excel thread open in maximized state or normal state explicitly then bingo we successfully resolved the issue.

Still it is a good practice to surround the exception causing snippet with try block and catch the exception so that there is no exceptional flow change in your application.

Unable to Set Freeze Panes property of Windows Class in c#
Unable to Set Freeze Panes Exception


Let us look at the source code for the same


Source Code


//Maximize the window state before setting freeze panes property

ExcelApp.Application.ActiveWindow.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
                 ExcelApp.Columns.AutoFit();
                    try
                    {
                        ExcelApp.Application.ActiveWindow.FreezePanes = false;
                        ExcelApp.Application.ActiveWindow.SplitColumn = 1; //To freeze first column
                        ExcelApp.Application.ActiveWindow.SplitRow = 1; // To freeze Top Row
                        ExcelApp.Application.ActiveWindow.FreezePanes = true;
                      }

                       catch(Exception ex)
                       {

                       }



Freeze Panes Exception

9 comments: