Reading text format from Excel using Microsoft.Office.Interop.Excel

2

I am trying to read text format e.g. the Strikethough property via

myworksheet.Cells[row, col].DisplayFormat.Style.Font.Strikethrough;

However, the result is always false, not matter what the actual formatting is.

Right before that, I read the value of the same cell using:

myworksheet.Cells[row, col].Value;

And get the correct value.

When I try using the debugger to read myworksheet.Cells[row, col].DisplayFormat.Style.Font.Strikethrough I receive false.

When I try using the debugger to read the format's Font and see its properties I get:

screenshot

Exception details:

Strikethrough {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)) --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Dynamic.IDispatchComObject.GetMembers(IEnumerable`1 names)} System.Reflection.TargetInvocationException

I have Office 2010 x86 installed on a Windows 7 SP1 x64 OS and I'm referencing Microsoft.Office.Interop.Excel version 14 from a .NET 4.0 project.

I created the .xlsx file programmatically using the same library and added the text formatting manually via Excel's UI.

The exception thrown by the access via debugger (watch and immediate) hints that the library version may be out of date, however, version 14 seems to be the correct version (http://www.microsoft.com/en-us/download/details.aspx?id=3508).

What am I missing?

.net
excel
office-interop
asked on Stack Overflow Jul 25, 2012 by Danny Varod

2 Answers

2

Found the cause and solution...

Apparently, the format is per character, not per cell.

Checking instead if there are any cells that have strikethrough formatting works:

for (int ch = 1; ch <= cell.Characters.Count; ch++)
{
    if (cell.Characters[ch, 1].Font.Strikethrough)
    {
        hasStrikeThrough = true;
        break;
     }
}

However, this only works is the cell's value is a string (doesn't work if value is bool for instance). - By works only for strings, I mean that for other formats I get an exception.

answered on Stack Overflow Jul 25, 2012 by Danny Varod
1

In VBA you'd typically just call Cells(r,c).Font directly, without the Style.

answered on Stack Overflow Jul 25, 2012 by Tim Williams

User contributions licensed under CC BY-SA 3.0