我已將 Web 瀏覽器控制元件添加到 Microsoft Access 表單。我讓它導航到一個帶有我可以點擊的附加鏈接的頁面。我的問題是,當我單擊一個鏈接時,它會在我的本地 Web 瀏覽器(Edge)中打開該鏈接。我希望新頁面保留在我的 MS Access 應用程式中并在同一個 Web 瀏覽器控制元件中打開。
該網站的 HTML 鏈接基本上看起來像這樣<a href="#" target="_blank">,它會打開一個新視窗(在我的情況下是一個全新的瀏覽器),我無法編輯該網頁。
有人會碰巧為我提供解決方案嗎?太感謝了!
uj5u.com熱心網友回復:
這是一個使用包含鏈接的 URL 的基本示例Target="_self"
Private Sub UserForm_Activate()
WebBrowser1.Navigate "https://www.dofactory.com/html/target/blank"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim doc As Object, links As Object, link As Object, t
Set doc = WebBrowser1.Document
Set links = doc.getelementsbytagname("A") 'get all links
For Each link In links 'loop over links
t = link.getAttribute("target") 'read the "target" attribute
If Not IsNull(t) Then 'has a target ?
If t = "_blank" Then 'target is "_blank" ?
link.removeAttribute "target" 'unset the target
Debug.Print "Removed _blank"
End If
End If
Next link
End Sub
您可能不會在您的 VBA 語言指南中找到任何 HTML 檔案物件模型方法:這些屬于“網路內容”領域和一組不同的 COM 參考。
uj5u.com熱心網友回復:
這是我用來獲得我想要的結果的 VBA 代碼。我確實從另一個外部來源獲得了幫助。在下面的示例中,Web 瀏覽器控制元件的名稱為 WebBrowser0。
Dim mParentURL As String
Private Sub Form_Load()
mParentURL = Me.WebBrowser0.ControlSource
End Sub
Private Sub WebBrowser0_DocumentComplete(ByVal pDisp As Object, URL As Variant)
WebBrowser0.Object.Document.Body.innerhtml = Replace(WebBrowser0.Object.Document.Body.innerhtml, "_blank", "_self", , , vbTextCompare)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/482482.html
上一篇:SQL查詢顯示2個不同表的行
