WebBrowser控件中的Excel自动化

时间:2013-10-02 17:15:45

标签: vb.net excel excel-vba automation vba

我使用以下代码在VB.net 2010 Express中的WebBrowser控件中显示Excel电子表格。

Option Explicit On
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Core
Imports System.Data
Public Class CustomerService
      Dim oDocument As Object
      Dim oXL As Excel.Application
      Dim oWB As Excel.Workbook
      Dim oSheet As Excel.Worksheet
      Dim oRng As Excel.Range
      Private Sub CustomerService_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      WebBrowser1.Navigate(quoteFile)
End Sub
Private Sub WebBrowser1_NavigateComplete2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles WebBrowser1.NavigateComplete2
    On Error Resume Next
    Dim oXL As New Excel.Application
    oDocument = e.pDisp.Document
    With oDocument.Application.CommandBars("Standard")
        .Position = 4 '[msoBarFloating]
        .Visible = True
    End With
    MsgBox("File opened by: " & oDocument.Application.Name)
    oDocument.Range("A1") = "HEllo"
End Sub
End Class

我的问题是,如何在WebBrowser1_NavigateComplete2方法中引用打开的文档,这样我就可以自动执行excel来说明特定单元格中的显示文本。

试过:

Option Explicit On
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Core
Imports System.Data
Public Class CustomerService
      Dim oDocument As Object
      Dim oXL As Excel.Application
      Dim oWB As Excel.Workbook
      Dim oSheet As Excel.Worksheet
      Dim oRng As Excel.Range
      Private Sub CustomerService_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      WebBrowser1.Navigate(quoteFile)
End Sub
Private Sub WebBrowser1_NavigateComplete2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles WebBrowser1.NavigateComplete2
    On Error Resume Next
    Dim oXL As New Excel.Application
    oDocument = e.pDisp.Document
    objApp = oDocument
    objBooks = objApp.Workbooks
    objBook = objBooks.Add
    objSheets = objBook.Worksheets
    objSheet = objSheets(1)
    oXL = oDocument(.Document) 'with or without .Document
    With oDocument.Application.CommandBars("Standard")
        .Position = 4 '[msoBarFloating]
        .Visible = True
    End With
    MsgBox("File opened by: " & oDocument.Application.Name)
    objSheet.Range("A1") = "HEllo"
End Sub
End Class

没有运气。如果你有一种方法可以采取这种方式,我很满意。

由于

1 个答案:

答案 0 :(得分:1)

更改最后一行以包含工作表是否解决了问题?

oDocument.worksheets(1).Range("A1") = "HEllo"

您是收到错误还是只是没有做任何事情?你得到你的msgbox吗?