我目前正在使用Word插件。从功能区启动Web浏览器控件。如果后者已经打开,我想修改文本框的内容并模拟按钮单击。问题是,当我尝试访问文本框时出现NullPointerException。我似乎有一些类似的问题,但是没有一种解决方案对我有用。这是我的代码:
Private Sub btn_Click(sender As Object, e As RibbonControlEventArgs) Handles btn.Click
Dim frame As New frm_browser()
Dim search As String
Dim url As String
Dim WordApp As Microsoft.Office.Interop.Word.Application = Globals.ThisAddIn.Application
Dim doc As HtmlDocument = frame.browser.Document
Dim htmlSearchField As HtmlElement
Dim htmlSearchButton As HtmlElement
search = WordApp.Selection.Text
If IsFormOpen(browser.GetType()) = False Then
frame.Show()
url = "http://example.com?search=" + search
frame.browser.Navigate(url)
Else
'Trigger the error and the htmlElement returns null
htmlSearchField = doc.GetElementById("tb_id")
htmlSearchField.SetAttribute("value", search)
htmlSearchButton = doc.GetElementById("btn_id")
htmlSearchButton.InvokeMember("click")
End If
End Sub
这是我尝试访问的html部分:
<input id="tb_id" name="xxx" type="text" value="">
<div id="btn_id">Contains an image that simulate a search button</div>
任何提示都会受到赞赏。谢谢您的帮助!