none
Excel的问题: RRS feed

  • 问题

  • 用WebBrowser打开Excel文档,无法在VB.NET中编写代码把Excel事件写进Excel文档,请帮我看一下是怎么一回事:
    -------------------------------------------------------------------------------------
    Dim Txls As New Excel.Application

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            WebBrowser1.Navigate("d:\test.xls")
    End Sub


    Private Sub WebBrowser1_NavigateComplete2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles WebBrowser1.NavigateComplete2

            Dim oDocument As Object = e.pDisp.GetType.InvokeMember("Document", System.Reflection.BindingFlags.GetProperty, Nothing, e.pDisp, Nothing)
            Dim oApplication As Object = e.pDisp.GetType.InvokeMember("Application", System.Reflection.BindingFlags.GetProperty, Nothing, oDocument, Nothing)
            Txls = CType(oApplication, Microsoft.Office.Interop.Excel.Application)

     End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim lngLines As Long
            Dim VBC As Microsoft.Vbe.Interop.VBComponent
            VBC = Txls.ThisWorkbook.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)
            On Error Resume Next
            With VBC.CodeModule
                lngLines = .CreateEventProc("BeforeSave", "Workbook") + 1
                .InsertLines(lngLines, "msgbox""This is test!!!""")
            End With
    End Sub
    -------------------------------------------------------------------------------------


    在点击Button2按纽时出现错误:
    错误指指向语句:
    VBC = Txls.ThisWorkbook.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)

    错误提示:
    未处理Comexception
    异常来自 HRESULT:0x800A03EC

    2010年3月1日 1:38