limit the number of opened IE 7/8/9 tabs

Posted on 2011-09-26
Last Modified: 2013-12-17
Is there reg key or value or a VB / VBS / C# code to limit the number of opened tabs in IE?

and if not, is there a way to close a specific IE tab using VB or C# ?
Question by:OrenRozen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 29

Accepted Solution

nffvrxqgrcfqvvc earned 500 total points
ID: 36602593
I don't know if there is a setting at all to limit tabs but you can try something like this...
Imports System.Runtime.InteropServices
Imports System.Threading

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    End Sub
End Class

Public NotInheritable Class TabBusting

    Private Shared _shell As Object = Nothing
    Private Shared _windows As Object = Nothing
    Private Shared _timer As Threading.Timer = Nothing
    Private Shared _maxTabs As UInteger = 0

    Public Shared Sub BeginBust(ByVal maxTabs As UInteger)
        _shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), True)
        _maxTabs = maxTabs
        ' Setup tab busting every second.
        _timer = New Threading.Timer(AddressOf BustWindows, Nothing, New TimeSpan(0, 0, 0), New TimeSpan(0, 0, 1))
    End Sub

    Public Shared Sub EndBust()
        _timer = Nothing
        _shell = Nothing
    End Sub
    Private Shared Sub BustWindows(ByVal state As Object)
        Dim windowList As New List(Of Object)
        Dim i As Integer

        ' Populate window collection
        _windows = _shell.Windows

        ' Filters ShellView windows
        For Each obj As Object In _windows
            If obj.Document.GetType.Name.Contains("HTMLDocument") Then
            End If
        If windowList.Count >= _maxTabs Then
            For i = 0 To windowList.Count - 1
                If i >= _maxTabs Then
                    windowList(i).Quit() ' bust tabs above maximum allowed.
                End If
            Next i
        End If

        windowList = Nothing
        _windows = Nothing

    End Sub
End Class

Open in new window


Author Comment

ID: 36711271
Thanks for the code. I'm trying to make it work but there is an error in line 41.
the object _shell does not contain a definition for windows.
Am I missing a reference?
LVL 29

Expert Comment

ID: 36711937
Nope no reference required but I haven't got that error when I run the test.... You converted to C# or using VB? Is this a *real error* or only a warning?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 36712157
converted to C#
LVL 29

Expert Comment

ID: 36712252
Oh okay... In VB we can use late binding but in C# you probrably have to use reflection like InvokeMember()...

Author Comment

ID: 36712299
do you have a c# code for that? or do you recommend for me to open a new question on the convertion?
LVL 29

Expert Comment

ID: 36712329
You can test the example "as is" in VB.NET and if it does what you intend then someone could probably convert it for you. I just whipped it up quickly in VB because you listed all three ( VB, VBS or C#)... Give it a try first though

Author Comment

ID: 36712725
working great in VB.
I didn't ask at the begining, but what I need is to run this application once on a terminal and monitor to close the tabs for ieplore ran by all users on that TS. I'd like to avoid running this application for every user.

LVL 29

Expert Comment

ID: 36713170
Hmmm... Not sure about terminal but here is a VBS example of the same concept if you can execute that someway under terminal.
Const MAX_TABS = 5 '// Set max tabs allowed opened.

Dim objShell
Dim objWindows

Call BeginBust

Sub BeginBust()

  Dim ieWindowCount
  Dim obj
  Dim i
  Dim arrBust(1024)

  Set objShell = Wscript.CreateObject("Shell.Application")


  Set objWindows = objShell.Windows
  ieWindowCount = 0

  For Each obj In objWindows
    If InStr( 1, TypeName(obj.Document), "HtmlDocument", 1) <> 0 Then
      ieWindowCount = CLng((ieWindowCount + 1))
      Set arrBust(ieWindowCount) = obj
    End If

  If (ieWindowCount > MAX_TABS) Then
    For i = 0 To ieWindowCount - 1
      If (i >= MAX_TABS) Then
	arrBust(i).Quit '// Bust tabs.
      End If
   MsgBox "Busted Tabs!"
  End If

  Erase arrBust
  WScript.Sleep 1000


End Sub

Open in new window


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 59
Need help converting bitmap to image in VB.Net 8 45
ModalPopup  question 22 38 Filesystem watcher not working 5 37
Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question