I have added a Web Browser Control to a Microsoft Access form. I have it navigating to a page with additional links that I can click on. My problem is that when I click a link, it opens the link in my local web browser (Edge). I want the new page to stay in my MS Access application and open in the same Web Browser Control.
The website's HTML link basically looks like this <a href="#" target="_blank">
which opens a new window (in my case a whole new browser), and I don't have the ability to edit that webpage.
Would anyone happen to have a solution for me? Thank you so much!
CodePudding user response:
Here's a basic example using a URL which contains a link with 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
You likely won't find any HTML Document Object Model methods in your VBA language guide: these belong to the "web stuff" sphere and to a different set of COM references.
CodePudding user response:
Here is the VBA code I used to get result I wanted. I did get help from another outside source. The Web Browser Control has a name of WebBrowser0 in the example below.
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