Excel Userform WebBrowser stop Newwindow

Ryan0120

New Member
Joined
Dec 9, 2019
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I got an issue with Excel userform Microsoft webbrowser.

Purpose of my userform/macro:
- Searching cell value in Webbrowser to view info on specified websites.

VBA Code:
Webbrowser1.Navigate = "https://www.digikey.nl/products/en?keywords=" & textbox1.text

The problem is that:
- I can't follow a link which normally would open in a new tab. (If I copy the link manually and browse it with the webbrowser it works fine.)
- I'm unable to catch the link its trying to follow it only cathces the url its currently at.
- It tries to open an newwindow instead it should open in current window.

Link to try:

then navigate to Datasheet button.

What I tried:
Catching the URL at BeforeNavigate event
Code:
Link = Webbrowser1.locationURL
or WebBrowser1.URL.ToString (didnt get the right link)

Redirecting at WebBrowser1_NewWindow2
Code:
Cancel = true
WebBrowser1.Navigate (link)

Some pointers in the right direction would be more then welcome.
Most results I found on google are c# based.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Ryan0120

New Member
Joined
Dec 9, 2019
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
No one who can point me into the right direction?
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,052
Try this code in the userform's module:
VBA Code:
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    
    Dim link As Object
    
    Set link = WebBrowser1.Document.ActiveElement
    If link.tagName = "A" Then
        WebBrowser1.Navigate link.href
    End If
    
    'Set Cancel flag to prevent new IE window opening
    Cancel = True

End Sub
 
Solution

Ryan0120

New Member
Joined
Dec 9, 2019
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Try this code in the userform's module:
VBA Code:
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
   
    Dim link As Object
   
    Set link = WebBrowser1.Document.ActiveElement
    If link.tagName = "A" Then
        WebBrowser1.Navigate link.href
    End If
   
    'Set Cancel flag to prevent new IE window opening
    Cancel = True

End Sub
Works like a charm so far thanks alot!!!:) You made my day !
 

Forum statistics

Threads
1,144,574
Messages
5,725,076
Members
422,590
Latest member
Mikeyyy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top