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:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
@KeepTrying, in nutshell you cannot control/track if user have clicked on anything in the page. for things like this you can go for UFT/QTP, selenium, iMacro for web automation with VBA. Else you'll have to build API using other languages.

regards.
 
Upvote 0
Dear VbaA2z,
Thanks for your suggestions. My first thinking was for API as I want to avoid using external products. There are some API gurus here (like Ivan F Moala, Jaafar, ZVI etc.) so I hope someone will jump in.
Regards,
 
Last edited:
Upvote 0
Bump... Any ideas please?
I thought about to add a command button to the userform (on top of the WebBrowser control) and assign macro to that. So if I open web page on the userform the button will be shown as part of the site (of course button should look like the same as it'd be part of that web page) but this is everything but professional.
 
Upvote 0
May I know what are you exactly trying to achieve by this? Is this to login to a web page?
 
Upvote 0
I'm developing my own site so I'd show that site on the userform and simulate to run macro on it.
 
Upvote 0
why do you want to 'detect when user click on a button on that Web page'? please provide more information because there might be another way of doing this?
 
Upvote 0
My question is about the concept, I'd like to know if it's possible at all. If yes, possibilities are endless :)
 
Upvote 0
The concept is that to demonstrate your webite properly, it should be done on a browser, not on a spreadsheet program like Excel.
 
Last edited:
Upvote 0
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)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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