NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.
MIT License
697 stars 143 forks source link

On an older release we suddenly experience issues #335

Closed Tdue21 closed 2 years ago

Tdue21 commented 2 years ago

Hello,

After some research, I've found this repository, and am wondering if I am in the right spot. I've inherited a project by a former coworker, which is now returning errors. This project is using NetOffice APIs for creating Excel sheets. It is version 1.7.2.

My customer is using this project with Office 365 (version 2109 Build 16.0.14430.20292) 64 bit. On my own machine Office 365 is Version 2110 Build 16.0.14527.20270 64-bit.

When the customer is using the project he received this exception:

System.Runtime.InteropServices.COMException (0x80004005): See inner exception(s) for details. 
    ---> System.Reflection.TargetlnvocationException: Exception has been thrown by the target of an invocation. 
    ---> System.Runtime.InteropServices.COMException: Unable to set the FreezePanes property of the Window class 

--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object0 args, Boolean 0 byrefModifiers, Int32 culture, Strings namedParameters) 
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object0 providedArgs, ParameterModifier0 modifiers, CultureInfo culture, String namedParams) 
at NetOffice.Invoker.PropertySet(COMObject comObject, String name, Object0 value) at NetOffice.Invoker.PropertySet(COMObject comObject, String name, Object0 value) 
at CustomerCreation.Control.ExcelController.KundeSheet(MappingItem 0 headers, Workbook wilp, String kundeOprettelsesSkema) 

The code looks like this:

private static void KundeSheet(MappingItem[] headers, Workbook wb, string kundeOprettelsesSkema)
{
    const int rowIndex = 1;

    using (var ws = (Worksheet)wb.Worksheets[1])
    {

        ws.Name = kundeOprettelsesSkema;
        var columnIndex = 1;

        foreach (var headerItem in headers.Where(x => x.UserField).OrderBy(x => x.Index))
        {
            var cell = ws.Cells[rowIndex, columnIndex++];
            cell.Value = headerItem.Name;
            cell.Font.Bold = true;
        }

        var name = ws.CellAddress(1, headers.Count(x => x.UserField));
        var range = ws.Range("$A1:$" + name);

        range.Columns.AutoFit();
        range.Columns.EntireColumn.NumberFormat = "@";

        ws.Application.ActiveWindow.SplitRow = 1;
        ws.Application.ActiveWindow.FreezePanes = true; // <-- It is here the exception occurs.

        range.EntireColumn.Locked = false;
        range.Locked = true;
    }
}

Does anyone have any idea, why this is happening?

jozefizso commented 2 years ago

Hi @Tdue21, this depends on the Excel release, installed add-ins and other local Excel configuration. The behavior you see is executed by the Excel, the library is a mere wrapper calling Excel API. So any behavioral changes are based on the Excel used.

I would try the StackOverflow, there are several questions about FreezePanes: https://stackoverflow.com/search?q=FreezePanes

Tdue21 commented 2 years ago

Ok, thanks :)