Vba: IE, closing a tab

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm navigating a web site. I'm stuck with this (I've to close a tab):

Code:
a id="zdres" class="closetab" on click="return false;"></a>

The id always changes.
It seems to be the only a and the only class="closetab" in the page.

Any suggestions?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Is it an actual IE tab or something on the web page? If the latter then try this:

Code:
IE.document.getElementsByClassName("closetab")(0).Click
 
Upvote 0
I've already tried with the following error:

error 438 object doesn't support this property or method

Probably it is a IE tab.


Now, from a wider point of view I think I've a problem of approach: this is not a conventional web page (or atleast, it's not for me).


The web address is something like this (unfortunately I cannot share it because it's not a public site):

https://wwwww.yyyyyy.zz/xxx/zulpages/menu.zul


If I open the html source, I see this at the beginning of the rows:

['zul.
 
Last edited:
Upvote 0
Hi,

as you only have one anchor tag you could try to get the id using something along the lines of:

Code:
    Set cls = IE.Document.getElementsByTagName("a")
    Attr = cls(0).getAttribute("ID")

I don't know whether that will help you with the actual problem of closing the page as 'on_click=false' suggest to me that whatever 'closetab' does won't happen.
 
Last edited:
Upvote 0
I've already tried with the following error:

error 438 object doesn't support this property or method

Probably it is a IE tab.
Which line causes that error?

If it is an IE tab, the following function can be called to find a specified tab:

Code:
Private Function Get_IE_Window2(Optional partialURLorName As String) As Object

    'Look for an IE browser window or tab already open at the specified (partial) URL or location name and, if found,
    'return that browser as an InternetExplorer object.  Otherwise return Nothing

    Dim Shell As Object
    Dim IE As Object
    Dim i As Variant 'Must be a Variant to index Shell.Windows.Item() array
    
    Set Shell = CreateObject("Shell.Application")
    
    i = 0
    Set Get_IE_Window2 = Nothing
    While i < Shell.Windows.Count And Get_IE_Window2 Is Nothing
        Set IE = Shell.Windows.Item(i)
        If Not IE Is Nothing Then
            If TypeName(IE) = "IWebBrowser2" And IE.LocationURL <> "" And InStr(IE.LocationURL, "file://") <> 1 Then
                If InStr(IE.LocationURL, partialURLorName) > 0 Or InStr(IE.LocationName, partialURLorName) > 0 Then
                    'Return the specified IE tab
                    Set Get_IE_Window2 = IE
                End If
            End If
        End If
        i = i + 1
    Wend
    
End Function
Call it like this:
Code:
    Dim IEtab As Object
    Set IEtab = Get_IE_Window2("https://wwwww.yyyyyy.zz/xxx/zulpages/menu.zul")  'or any part of the URL or location name
    If Not IEtab Is Nothing Then
        IEtab.Quit
        MsgBox "Closed " & IEtab.LocationURL
    Else
        MsgBox "IE tab for https://wwwww.yyyyyy.zz/xxx/zulpages/menu.zul not found"
    End If
 
Upvote 0
I'm sorry, I did not explain myself well.
Now, I've tried to reproduce graphically the situation I'm dealing with. This is the situation after the login operation.

https://imgur.com/a/lApH6

Before doing any actions (if I click ACTIONS, a menu will be opened), I've to close the tab "Elaborate".


This is the html code (the id changes for any access):

a id="zdres" class="closetab" on click="return false;">


The instrucion
IE.document.getElementsByClassName("closetab")(0).Click

produces the error:
error 438 object doesn't support this property or method
 
Last edited:
Upvote 0
So it isn't an IE tab after all.

See if this works (it has 2 ways of clicking the link and one is commented out). You must set a reference to Microsoft HTML Object Library in Tools-References.

Code:
Dim HTMLdoc As HTMLDocument
Dim evtClick As Object
Dim link As HTMLAnchorElement
Dim i As Long

Set HTMLdoc = IE.document
Set evtClick = HTMLdoc.createEvent("HTMLEvents")
evtClick.initEvent "click", True, False

Set link = Nothing
i = 0
While i < HTMLdoc.links.length And link Is Nothing
    Debug.Print i, HTMLdoc.links(i).className, ">" & HTMLdoc.links(i).innerText & "<"
    If HTMLdoc.links(i).className = "closetab" And HTMLdoc.links(i).innerText = "Elaborate" Then Set link = HTMLdoc.links(i)
    i = i + 1
Wend
If Not link Is Nothing Then
    'Either
    link.Click
    'Or
    link.dispatchEvent evtClick
Else
    MsgBox "Link not found"
End If
That's all I can suggest without knowing the URL or accessing to the site. The Debug.Print statement helps you to confirm that the code is referencing the correct element.
 
Upvote 0
So it isn't an IE tab after all.

See if this works (it has 2 ways of clicking the link and one is commented out). You must set a reference to Microsoft HTML Object Library in Tools-References.

Code:
Dim HTMLdoc As HTMLDocument
Dim evtClick As Object
Dim link As HTMLAnchorElement
Dim i As Long

Set HTMLdoc = IE.document
Set evtClick = HTMLdoc.createEvent("HTMLEvents")
evtClick.initEvent "click", True, False

Set link = Nothing
i = 0
While i < HTMLdoc.links.length And link Is Nothing
    Debug.Print i, HTMLdoc.links(i).className, ">" & HTMLdoc.links(i).innerText & "<"
    If HTMLdoc.links(i).className = "closetab" And HTMLdoc.links(i).innerText = "Elaborate" Then Set link = HTMLdoc.links(i)
    i = i + 1
Wend
If Not link Is Nothing Then
    'Either
    link.Click
    'Or
    link.dispatchEvent evtClick
Else
    MsgBox "Link not found"
End If
That's all I can suggest without knowing the URL or accessing to the site. The Debug.Print statement helps you to confirm that the code is referencing the correct element.

I'm making some attempts.

At the moment the result is

"Link not found"

The Debug.print:

Debug.Print HTMLdoc.Links.Length
2

Debug.Print i
0 ><
1 main_menu >LOG OUT <
 
Last edited:
Upvote 0
Does the page use frames? Search the source for "frame" to find out. If so, you first need to access the HTMLDocument of the specific frame. There should be code on the forum showing how to reference HTML frames.
 
Upvote 0
It doesn't seem so.

The first row is:


< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,069
Members
449,090
Latest member
fragment

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