COMException occurred - Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME)) when trying to modify an existing excel file

1

3rd line returns COMException. oBooks.GetType().Invoke...

Dim oBooks As Microsoft.Office.Interop.Excel.Workbook = Me.fOpenXlsFile(strXLSFile)

Dim ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
oBooks.GetType().InvokeMember("Add", Reflection.BindingFlags.InvokeMethod, Nothing, oBooks, Nothing, ci)

For i As Int32 = 0 To objLv.Items.Count - 1
    oBooks.Styles.Item(i + 1).Interior.Color = objLv.Items(i).BackColor
Next
oBooks.Save()
vb.net
excel
com
asked on Stack Overflow Aug 29, 2016 by Tassisto

1 Answer

3

You're trying to add a Workbook to another Workbook. You need to add it to the Workbooks collection. Something like this should work:

Dim wbs As Excel.Workbooks = oBooks.Application.Workbooks

wbs.GetType().InvokeMember("Add", Reflection.BindingFlags.InvokeMethod, Nothing, wbs, Nothing, ci)
answered on Stack Overflow Aug 29, 2016 by Jim Hewitt • edited Aug 29, 2016 by Jim Hewitt

User contributions licensed under CC BY-SA 3.0