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

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# ?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

nffvrxqgrcfqvvcConnect With a Mentor Commented:
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

OrenRozenAuthor Commented:
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?
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?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

OrenRozenAuthor Commented:
converted to C#
Oh okay... In VB we can use late binding but in C# you probrably have to use reflection like InvokeMember()...
OrenRozenAuthor Commented:
do you have a c# code for that? or do you recommend for me to open a new question on the convertion?
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
OrenRozenAuthor Commented:
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.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.