发生COMException - 未知名称。 (尝试修改现有excel文件时{HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME))

时间:2016-08-29 07:47:54

标签: vb.net excel com

第3行返回COMException。 oBooks.GetType()。调用...

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()

1 个答案:

答案 0 :(得分:3)

您正在尝试将工作簿添加到另一个工作簿。您需要将其添加到Workbooks集合中。这样的事情应该有效:

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

wbs.GetType().InvokeMember("Add", Reflection.BindingFlags.InvokeMethod, Nothing, wbs, Nothing, ci)