I have declared an object named IE in my VBA code.
Set IE = CreateObject("InternetExplorer.Application")
Sometimes user may close the browser linked to IE object. How can i write code to find if the browser is closed or open?
As shown in below image, software shows Internet Explorer when i place cursor on IE object during run time. Can this value be used to determine if browser is closed or open? If not, any other way?
Create a custom class to capture the Internet Explorer events using WithEvents. You'll need to set a reference to Microsoft Internet Controls.
Option Explicit
Private WithEvents IE As InternetExplorer
Private Sub IE_OnQuit()
End Sub
Private Sub IE_OnVisible(ByVal Visible As Boolean)
End Sub
Private Sub Class_Initialize()
Set IE = New InternetExplorer
End Sub
If the object is closed, then accessing some of it's members should give you an error.
Dim isOpen As Boolean ' False by default
On Error Resume Next
If IE.HWND > 0 Then isOpen = True
On Error Goto 0
Related
I am trying to instantiate an existing form (frmVisibleForm) in my project from within a custom class module (clsMBox) and manipulate its properties from there too. I want to be able to use events from the form.
What I expect to happen:
The Form frmVisibleForm is instantiated but invisible
The Form gets set to Modal
The Form gets set to Visible
The Form gets Focus
What happens:
Nothing. No form shows up, no error messages, no prompts, nothing happens at all when running the test module´s function. Its my first time trying out custom classes in access so maybe I made some fundamental error but I can't figure out why it doesn´t work the way I thought it would. Appreciate any help.
Here is the code I have so far:
The Form (frmVisibleForm):
Option Compare Database
Option Explicit
Public Event DataInput(InputValue As String)
(No actual events thus far)
The Custom Class Module (clsMBox):
Option Compare Database
Option Explicit
Dim WithEvents cls_frmVisibleForm As Form_frmVisibleForm
Private Sub InstantiateForm()
Set cls_frmVisibleForm = New Form_frmVisibleForm
With cls_frmVisibleForm
.Modal = True
.Visible = True
.SetFocus
End With
End Sub
The Module I try to test it from (mdlTestMBox):
Option Compare Database
Option Explicit
Public Function ClassTest()
Dim mbox As clsMBox
Set mbox = New clsMBox
End Function
I guess you need to either make InstantiateForm a public method, and then call that, or rename it to initialise:
Private Sub Class_Initialize()
Static cls_frmVisibleForm As Access.Form
Set cls_frmVisibleForm = New Form_frmVisibleForm
With cls_frmVisibleForm
.Modal = True
.Visible = True
.Move 0, 0
End With
End Sub
To open and close an instance of the form:
Public Function ClassTest()
Static mbox As clsMBox
Set mbox = New clsMBox
Stop
DoCmd.Close acForm, Forms(0).Name
End Function
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 have problems with sheets.activate contained into Workbook_Open() when the file is in protected mode (because downloaded from internet). As soon as you "Enable Editing" the Workbook_Open() is executed and the error comes out.
If I use this code:
Private Sub Workbook_Open()
Sheets(2).Activate
End Sub
I have this error:
Runtime Error 1004 Activate method of Worksheet class failed
Reading in some other discussion i tried using the Workbook_Activate() method which works if I have only one simple activation in all my project. The previous example could be fixed if I use:
Private Sub Workbook_Open()
a = True
End Sub
Private Sub Workbook_Activate()
If a Then
Sheets(2).Activate
a = False
End If
End Sub
but it partially fix the problem, this code could work but next time i have another sheets.activate in my project the error comes out again, (i.e. if i click a button into a panel or if i run other routines).
This error comes out only the first time you open the file, if you stop the debugger, close the file without saving and reopen the file the error doesn't come out again but i would avoid it to come out the first time
Thanks in advance
This looks like it is a known issue:
Object Model calls may fail from WorkbookOpen event when exiting Protected View
It says ...
Resolution
You can workaround the issue by either :-
If the location from where the workbooks are being open is trusted, add that location to the Excel's Trusted Locations.
Defer Object Model calls to outside of the WorkbookOpen event to WorkbookActivate event.
Code example to defer object model calls
Option Explicit
Public WithEvents oApp As Excel.Application
Private bDeferredOpen As Boolean
Private Sub oApp_WorkbookActivate(ByVal Wb As Workbook)
If bDeferredOpen Then
bDeferredOpen = False
Call WorkbookOpenHandler(Wb)
End If
End Sub
Private Sub oApp_WorkbookOpen(ByVal Wb As Workbook)
Dim oProtectedViewWindow As ProtectedViewWindow
On Error Resume Next
'The below line will throw error (Subscript out of range) if the workbook is not opened in protected view.
Set oProtectedViewWindow = oApp.ProtectedViewWindows.Item(Wb.Name)
On Error GoTo 0 'Reset error handling
If oProtectedViewWindow Is Nothing Then
bDeferredOpen = False
Call WorkbookOpenHandler(Wb)
Else
'Delay open actions till the workbook gets activated.
bDeferredOpen = True
End If
End Sub
Private Sub WorkbookOpenHandler(ByVal Wb As Workbook)
'The actual workbook open event handler code goes here...
End Sub
Check the file properties before you open it. If it comes from the internet, there is usually a "Unprotect" checkbox on the properties page you can check to unprotect it, then it should open like normal.
There is also a "Read-only" checkbox that might help you too. Make sure the file is not read only.
Just do this before you open it.
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.
Set Browser = New SHDocVw.InternetExplorer
Once you create a new browser instance, how do you refer to that instance, instead of closing and reopening, say If I activate Excel window, then I want to activate back to the browser, how is this done?
I looked into
AppActivate "Windows Internet Explorer"
But If I have more than one window open, that wont work right, I dont think
I think you mean:
Option Explicit
Public browser As SHDocVw.InternetExplorer
Sub NavigateTo()
Set browser = New SHDocVw.InternetExplorer
browser.Visible = True
browser.Navigate "http://stackoverflow.com"
End Sub
In other words, declare the browser variable at module level so it remains available.
You can also capture an instance like so:
Sub getIE()
Dim sh As Object, oWin As Object, IE As Object
Set sh = CreateObject("Shell.Application")
For Each oWin In sh.Windows
If TypeName(oWin.Document) = "HTMLDocument" Then
Set IE = oWin
Exit For
End If
Next
Debug.Print IE.Document.url
End Sub