0

我正在使用以下代码在 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

没有运气。如果你甚至有办法接受这个,我会全神贯注。

谢谢

4

1 回答 1

1

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

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

你得到一个错误还是它只是没有做任何事情?你得到你的msgbox了吗?

于 2013-10-02T18:34:47.617 回答