I have an Access 2010 Form with a WebBrowser control that I would like to remove a Submit button from the rendered HTML. My WebBrowser control works fine when I'm reading the HTML but I get an error when trying to change what's rendered. I've tried several methods and they all return an "Object Required" error. I have a lot of working code that uses the WebBrowser control so I'm hesitant to change it out for another control. Is there anyway of doing this with this control?
Below is a snippet of the latest code:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim strLook As String
Dim doc As HTMLDocument
Set doc = WebBrowser1.Document
Dim txtInner
strLook = "<input type=""submit"" name=""subaction"" value=""Force"" class=""inputfield"">"
txtInner = WebBrowser1.Document.Body.innerHTML
txtInner = Replace(txtInner, strLook, "")
Webrowser1.Document.Body.innerHTML = txtInner
End Sub
Thanks in advance!
Something like:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim doc As HTMLDocument
Dim els
Set doc = WebBrowser1.Document
Set els = doc.getElementsByTagname("input")
For Each el In els
If el.Value = "Force" And el.className = "inputfield" Then
el.ParentNode.RemoveChild el
Exit For
End If
Next el
End Sub
Related
I use SHDocVw.InternetExplorer to show in powerpoint a local html file to the user. The file contains a form with a submit button and I would like to run my vba function when the button is clicked. I did the following to handle the onQuit-event of the InternetExplorer.
Class module "CIE"
Public WithEvents ie As SHDocVw.InternetExplorer
Private Sub ie_onQuit()
Debug.Print "onQuit was fired"
End Sub
Module code
Option Explicit
Public curCIE As CIE
Sub open_ie()
Dim CIEobj As CIE
Set CIEobj = New CIE
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.Navigate "C:\search.html"
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set CIEobj.ie = ie
Set curCIE = CIEobj
End Sub
I actually want to handle the onclick event of
curCIE.ie.Document.getElementsByTagName("button").Item(0)
but I do not know how to proceed.
You could add reference to MSHTML, then make your class module this way:
Public WithEvents ie As SHDocVw.InternetExplorer
Private WithEvents button As MSHTML.HTMLButtonElement
Public Sub LoadButton()
Set button = ie.document.getElementsByTagName("button").Item(0)
End Sub
Private Function button_onclick() As Boolean
'do what you want here
End Function
Private Sub ie_onQuit()
Debug.Print "onQuit was fired"
End Sub
And then, in the other module, add this statement after setting CIEobj.ie property:
CIEobj.LoadButton
I am trying to set some command buttons properties out in bulk . That is trying to set various properties of the command buttons in one go rather than repeat the code for each command button individually.
The document has 30+ command buttons.
In the Class - I have put the code below:
Option Explicit
Public WithEvents cMDButtonGroup As CommandButton
Private Sub cMDButtonGroup_Click()
With cMDButtonGroup
If .Caption = "Press" Then
' Add some other button properties
Else
.Caption = " Complete"
End If
End With
In a VBA Module - I have put the code below:
Option Explicit
Dim Buttons() As New cMDButtonClass
Sub Buttons()
Dim ButtonCount As Integer
Dim ctl As Control
' Create the Button objects
ButtonCount = 0
For Each ctl In ActiveDocument.Controls ' This may be wrong
If TypeName(ctl) = "CommandButton" Then
ButtonCount = ButtonCount + 1
ReDim Preserve Buttons(1 To ButtonCount)
Set Buttons(ButtonCount).ButtonGroup = ctl
End If
End If
Next ctl
End Sub
The above may have been sourced from VBA Express? Unfortunately I have lost the link.
Unfortunately I do not know how to proceed to fix this.
Final Solution: Tim's Code works perfectly. You also need to load the buttons
Put the below code in ThisDocument
Private Sub Document_Open()
Call SetupButtons
End Sub
cMDButtonClass (simplified)
Public WithEvents oBtn As CommandButton
Private Sub oBtn_Click()
MsgBox "clicked: " & oBtn.Caption
End Sub
In a regular module:
Dim colButtons As New Collection '< simpler to manage than an array
Sub SetupButtons()
Dim ButtonCount As Integer
Dim ctl, c
Dim oB As cMDButtonClass
'Following Cindy's comment...
For Each ctl In ActiveDocument.InlineShapes
If Not ctl.OLEFormat Is Nothing Then
Set c = ctl.OLEFormat.Object
If TypeName(c) = "CommandButton" Then
Set oB = New cMDButtonClass
Set oB.oBtn = c
colButtons.Add oB
End If
End If
Next ctl
End Sub
In Microsoft Access 2010, I am trying to dynamically create a form and then add a commandbutton to it. However, I can't figure out how to assign an event handler to that button's click (or onclick) event?
From reading extracts on the internet, I have created the following vba module code:
Option Compare Database
Option Explicit
Public Sub ProduceForm()
Dim aForm As Form
Dim aButton As CustomButton
Set aForm = CreateAForm("Table1")
Set aButton = CreateAButton(aForm, "Click me!")
DoCmd.Restore
End Sub
Private Function CreateAForm(table As String) As Form
Set CreateAForm = CreateForm(, table)
With CreateAForm
.Caption = CreateAForm.Name
End With
End Function
Private Function CreateAButton(aForm As Form, text As String) As CustomButton
Set CreateAButton = New CustomButton
Set CreateAButton.btn = CreateControl(aForm.Name, acCommandButton)
CreateAButton.SetupButton text
End Function
In accordance with the internet advice, I have then added the following class module (that I've called "CustomButton" and is referred to above):
Option Compare Database
Public WithEvents btn As CommandButton
Public Sub SetupButton(text As String)
If IsNull(btn) = False Then
With btn
.Caption = text
.OnClick = "[Event Procedure]"
End With
End If
End Sub
Public Sub btn_OnClick() ' or should this method just be called btn_Click()?
MsgBox "Happy days"
End Sub
However, when I run this code and then click the button (when in form view) nothing happens?
I notice that explanations given for similar problems, but for excel 2010, give an alternative solution by writing code as a string in a "CodeModule", which I think is linked to the "vbComponents" object. This solution should work for me but I can't find this functionality in Access 2010?
Anyway, any help on this issue would be greatly appreciated.
Thanks
EDIT - my original answer had assumed that Access behaved something like excel, which doesn't seem to be the case...
You don't need a custom class to handle events - you just pass a string to the OnClick property of your button.
This works for me - maybe needs tidying up a bit though.
Public Sub ProduceForm()
Dim aForm As Form, strName As String
Dim btn As CommandButton
Set aForm = CreateAForm("Table1")
Set btn = CreateAButton(aForm, "Click me!", "=SayHello()")
btn.Top = 100
btn.Left = 100
Set btn = CreateAButton(aForm, "Click me too!", "=SayHello('World')")
btn.Top = 1000
btn.Left = 100
DoCmd.OpenForm aForm.Name, , , , , acDialog
DoCmd.Restore
End Sub
Private Function CreateAForm(table As String) As Form
Set CreateAForm = CreateForm(, table)
With CreateAForm
.Caption = CreateAForm.Name
End With
End Function
Private Function CreateAButton(aForm As Form, txt As String, proc As String) As CommandButton
Dim btn As CommandButton
Set btn = CreateControl(aForm.Name, acCommandButton)
btn.OnClick = proc
btn.Caption = txt
Set CreateAButton = btn
End Function
'has to be a Function not a Sub
Public Function SayHello(Optional arg As String = "")
MsgBox "Hello " & arg
End Function
Hi I am new to Visual Basic 6,
My application has a section which shows an advertisement. When the ad is clicked, the button "Power" is activated.
I tried to detect click on my ad that is inside a with the id "anuncio1".
So far I've only managed to detect when you click in the WebBrowser zone but does not detect when I've clicked on a link.
Then I let my code:
Option Explicit
'Reference to HTML DOM
Dim WithEvents HTMLDOC As HTMLDocument
Private Const READYSTATE_COMPLETE As Long = 4
'Load the website directly to the ad hiding Scrollbars and focusing anchor #anuncio1
Private Sub Form_Load()
WebBrowser1.Navigate "www.traductoramixer.es#anuncio1"
Do While WebBrowser1.Busy: DoEvents: Loop
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE: DoEvents: Loop
WebBrowser1.Document.parentWindow.scrollBy 25, 0
WebBrowser1.Document.body.Scroll = "no"
WebBrowser1.Document.body.Style.overflow = "hidden"
End Sub
'Deteck click in WebBrowser1 zone
Private Function HTMLDOC_onclick() As Boolean
Command1.Enabled = True
End Function
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If Not WebBrowser1.Document Is Nothing Then
Set HTMLDOC = WebBrowser1.Document
End If
End Sub
i suggest you:
1) create a Timer with Interval 100
2) and check WebBrowser1.LocationName string value frequently
if its value was not equal to the url your customer clicked on it, then be sure that your customer clicked on a wrong link
I need a VBA code which can do some action in VBA when the webpage frame is just about to navigate to other page.for example when i click on some link,button it navigates to other page i want to take screen shot of the page before frame navigates to other.i have done something like this but it is taking screen shot of the blank page and its only working for onetime as the page is navigated and object gets changed. please help me with this i have been searching for this since 2 weeks help me.
Sub pageLoad()
Set ie2 = GetIE("https://xyz.com")
Dim LinkFound As Boolean
Dim linkCollection
Dim IEfr0 As Object
i = 0
Dim Link As MSHTML.HTMLAnchorElement
'HTMLInputElement
Set wordapp = CreateObject("word.Application")
wordapp.Visible = True
Set wrdDoc = wordapp.Documents.Add
Set IEfr0 = ie2.document.frames(0).document
Set linkCollection = IEfr0.getElementsByTagName("a")
Do While ie2.Visible = True
For Each Link In linkCollection
If ie2.document.frames(2).document.readyState = "interactive" Then
sai1
End If
If IEfr0.readyState = 1 Then
sai1
End If
Next
Loop
End Sub
Sub sai1()
Application.SendKeys "{PRTSC}"
wordapp.Selection.Paste
Do While ie2.Busy
Loop
End Sub
Here's a possible solution. Only showing you how to create the object and specify the BeforeNavigate2 event. This requires you to reference Microsoft Internet Controls and create the IE object (this will not work with late binding). This code must be in an object module (worksheet or workbook) because you cannot use WithEvents in a standard module.
Option Explicit
Dim WithEvents ie As InternetExplorer
Sub Example()
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "Your URL Here..."
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
End Sub
Private Sub ie_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
'Place code to run before navigating here.
End Sub
An issue you may see with this is that for some websites, the BeforeNavigate2 event will actually fire multiple times due to additional calls for resources when loading.