Open Web page on a userform then detect when I click on a button on that Web page within userform

KeepTrying

Active Member
Joined
Aug 19, 2012
Messages
276
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Dear Experts,

I'd like to run macros on a Web page but as far as I know this is not possible or quite difficult. So I want to at least imitate it. My idea: create a userform, show a Web page on it then detect if e.g. a button was clicked on that Web page on the userform. Then run a macro after clicking (like click event).

I know if I add a "WebBrowser1" control I can easily show a Web page on a userform. Sample code:

Code:
Private Sub UserForm_Initialize()
With Me.WebBrowser1
    .Navigate "https://www.wikipedia.org/"
    .Width = Application.Width
    .Height = Application.Height
    .Top = Application.Top
    .Left = Application.Left
End With
End Sub

But how can I trigger if a button was clicked on that Web page on the userform? I know that controlling Web page is possible from VBA (by creating object called "InternetExplorer.Application") but my question is different.

Web page in the VBA code can be different of course.

Thank you in advance for your help.

Regards,

KeepTrying
 
Last edited:
There is a problem to post "on click" without space char.
In the triggering code for the macro replace asterisks "*" by the "on click" without space symbol.
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Actually it's possible to trigger macro in case button on web page was clicking.
Use this code for the learning purposes and adapt it for your need:
Rich (BB code):
Option Explicit
' ZVI:2017-01-19 How to run a macro at web page button click
' https://www.mrexcel.com/forum/excel-questions/985435-open-web-page-userform-then-detect-when-i-click-button-web-page-within-userform.html
 
' Put all the code into module of userform with Browser1.
' NOTE: Reference to Microsoft HTML Object library is required
 
Private WithEvents HtmlDoc As MSHTML.HTMLDocument
Dim WBName As String
 
Private Sub UserForm_Activate()
 
  Dim objWB As Object
  Dim Html As String
 
  ' Use WebBroeser1
  Set objWB = WebBrowser1
 
  ' or IE
  'Set objWB = CreateObject("InternetExplorer.Application")
 
  ' and this activates IE
  objWB.Visible = True
 
  ' Save name of objWB for event usage (activation of MsbBox window
  WBName = objWB.Name
 
  ' Simulate the site's code
  Html = _
    "(!DOCTYPE html)" & vbLf & _
    "(html)" & vbLf & _
    "(head)" & vbLf & _
    "(title)My site(/title)" & vbLf & _
    "(/head)" & vbLf & _
    "(body)" & vbLf & _
    "(input id='txt1' type='text' size='20' value='Type text here'/)" & vbLf & _
    "(input id='cmd1' type='submit' value='Click me!'/)" & vbLf & _
    "(h1)Welcome to my site!(/h1)" & vbLf & _
    "(p)Type any text and click the button to run macro(/p)" & vbLf & _
    "(/body)" & vbLf & _
    "(/html)"
 
  ' This is for the reason forum does not support symbols "<" and ">" in a code
  Html = Replace(Html, "(", "<")
  Html = Replace(Html, ")", ">")
  'Debug.Print Html
 
  ' Load the "about:" page (or the real site's page)
  objWB.Navigate "about:blank"
 
  ' Wait
  With objWB
    While .Busy = True: DoEvents: Wend
    While .ReadyState <> 4: DoEvents: Wend
    While .Document Is Nothing: DoEvents: Wend
  End With
 
  ' Write html code (skip this if real site was loaded)
  objWB.Document.write Html
 
  ' Set focus on input text field and select its text
  With objWB.Document.getElementById("txt1")
    .Focus
    .Select
  End With
 
  ' Set the triggering macro
  Set HtmlDoc = objWB.Document
 
End Sub
 
' This macro triggers at event of "cmd1" clicking on web page
Private Function HtmlDoc_*******() As Boolean
  If HtmlDoc.parentWindow.Event.srcElement.ID = "cmd1" Then
    If WBName Like "*Internet*" Then
      ' Activate Excel to show MsgBox in case IE was used insrtead of WebBrowser control
      Application.Visible = False
      Application.Visible = True
    End If
    ' Show the message
    MsgBox "The input text is: " & vbLf & vbLf & HtmlDoc.getElementById("txt1").Value, , "cmd1"
    ' ... Your code is here ...
  End If
End Function

Code simulates a site page with input edit field and a command button.
Type any text and click the button on that web page to run the macro.
Reference to Microsoft HTML Object library should be set.
In te code replace asterisks "*" by "*******" (the problem of this forum)

Dear Vladimir,

Thanks a lot for your effort, your code works like a charm. However I couldn't implement it for a real site (showing within WebBrowser control on a userform). I made one step back and first tried to identify and click on a button (called "Search New Posts") using IE object on MrExcel Forum. I added my code to a standard module and it ran successfully. But If I add that code to a userform (of course with WebBrowser control) it fails:

Code:
IE.Document.getElementsByClassName("navtab").Item(1).Click

Where IE means WebBrowser1. Unfortunately .focus and .select didn't work for that line above.

Shall I post my full code for the mentioned button?

Have a great day.

Regards,
 
Last edited:
Upvote 0
My answer was on the titled question - detect clicking on a button on Web page to run a macro.
Not sure why you are asking now for the reverse task – how to click on a button on Web page via a macro.
Hope you have full control on html code of your web site to implement the suggested code.
 
Last edited:
Upvote 0
...tried to identify and click on a button (called "Search New Posts")
To access the "Search New Posts" of MrExcel forum, simple navigate a WebBrowser to the url "https://www.mrexcel.com/forum/search.php?do=getnew&contenttype=vBForum_Post"
You can get this link from WebBrowser1.Document.all("vbtab_whatsnew").Children(0).href
 
Last edited:
Upvote 0
To access the "Search New Posts" of MrExcel forum, simple navigate a WebBrowser to the url "https://www.mrexcel.com/forum/search.php?do=getnew&contenttype=vBForum_Post"
You can get this link from WebBrowser1.Document.all("vbtab_whatsnew").Children(0).href
Thanks for your advice. What I wanted:
- open Mr Excel Forum page in WebBrowser1 on a userform - that's the easy part
- if I click on a button like "Search New Posts": a macro should run (for testing a message box is good enough)
I'll try to figure it out this weekend based on your brilliant sample above.
 
Last edited:
Upvote 0
- open Mr Excel Forum page in WebBrowser1 on a userform - that's the easy part
- if I click on a button like "Search New Posts": a macro should run (for testing a message box is good enough)
Here is the code:
Rich (BB code):
Option Explicit
' ZVI:2017-01-19 How to run a macro at web page button click
' https://www.mrexcel.com/forum/excel-questions/985435-open-web-page-userform-then-detect-when-i-click-button-web-page-within-userform.html
 
' Put all the code into the module of userform with Browser1.
' NOTE: Referense to Microsoft HTML Object library is required
 
Private WithEvents HtmlDoc As MSHTML.HTMLDocument
 
Private Sub UserForm_Activate()
 
  Dim objWB As Object
  Dim Html As String
 
  ' Use WebBroeser1
  Set objWB = WebBrowser1
 
  ' Load the site page
  objWB.Navigate "https://www.mrexcel.com/forum/"
 
  ' Wait
  With objWB
    Application.StatusBar = "Busy..."
    While .Busy = True: DoEvents: Wend
    Application.StatusBar = "Not ready..."
    While .ReadyState <> 4: DoEvents: Wend
    Application.StatusBar = "Load document ..."
    While .Document Is Nothing: DoEvents: Wend
    Application.StatusBar = False
  End With
 
  ' Set the triggering macro
  Set HtmlDoc = objWB.Document
 
End Sub
 
' This macro triggers at event of 'Search New Posts' clicking on MrExcel/Forum web page
' In this function name replace the asterisks by "on click" without space char
Private Function HtmlDoc_*******() As Boolean
  If HtmlDoc.parentWindow.Event.srcElement.className = "navtab" Then
    ' Show the message
    MsgBox "You click on 'Search New Posts'"
    ' ... Your code is here ...
  End If
End Function
 
Last edited:
Upvote 0
Please replace this line of the function's code:
If HtmlDoc.parentWindow.Event.srcElement.className = "navtab" Then
by that more correct one:
If HtmlDoc.parentWindow.Event.srcElement.outerText = "Search New Posts" Then

And then get the link via HtmlDoc.parentWindow.Event.srcElement.href
 
Last edited:
Upvote 0
Solution
Here is the code:
Rich (BB code):
Option Explicit
' ZVI:2017-01-19 How to run a macro at web page button click
' https://www.mrexcel.com/forum/excel-questions/985435-open-web-page-userform-then-detect-when-i-click-button-web-page-within-userform.html
 
' Put all the code into the module of userform with Browser1.
' NOTE: Referense to Microsoft HTML Object library is required
 
Private WithEvents HtmlDoc As MSHTML.HTMLDocument
 
Private Sub UserForm_Activate()
 
  Dim objWB As Object
  Dim Html As String
 
  ' Use WebBroeser1
  Set objWB = WebBrowser1
 
  ' Load the site page
  objWB.Navigate "https://www.mrexcel.com/forum/"
 
  ' Wait
  With objWB
    Application.StatusBar = "Busy..."
    While .Busy = True: DoEvents: Wend
    Application.StatusBar = "Not ready..."
    While .ReadyState <> 4: DoEvents: Wend
    Application.StatusBar = "Load document ..."
    While .Document Is Nothing: DoEvents: Wend
    Application.StatusBar = False
  End With
 
  ' Set the triggering macro
  Set HtmlDoc = objWB.Document
 
End Sub
 
' This macro triggers at event of 'Search New Posts' clicking on MrExcel/Forum web page
' In this function name replace the asterisks by "on click" without space char
Private Function HtmlDoc_*******() As Boolean
  If HtmlDoc.parentWindow.Event.srcElement.className = "navtab" Then
    ' Show the message
    MsgBox "You click on 'Search New Posts'"
    ' ... Your code is here ...
  End If
End Function
Thanks for not let me down :)
Code didn't work for me for Webrowser1 control, it seems that event is not triggered. But if I move (and slightly change) your code to a standard module and use Class module for "Private Function HtmlDoc_OnClic k() As Boolean" part then it does work. I tested it on company laptop maybe that's the reason of inconsistency.
Anyway, code is great and I'll spend some time to fully understand and a bit complete it.
 
Last edited:
Upvote 0
Please replace this line of the function's code:
If HtmlDoc.parentWindow.Event.srcElement.className = "navtab" Then
by that more correct one:
If HtmlDoc.parentWindow.Event.srcElement.outerText = "Search New Posts" Then

And then get the link via HtmlDoc.parentWindow.Event.srcElement.href
Thanks, that really does make sense. If I use
Code:
If HtmlDoc.parentWindow.Event.srcElement.className = "navtab" Then
then clicking on any buttons in the navigation area on the top fires the event.
 
Upvote 0
Glad it has helped.
Code was tested on my computers and it is working as is.
UserForm is a class module itself and isolation of the event part of the code into a separate class module is not required. But it's good to know you have managed it to work.
Have a good weekend! :)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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
Back
Top