Results 1 to 5 of 5

Thread: Counting Open work books

  1. #1

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Where i am
    Posts
    38

    Counting Open work books

    Very simple i am sure all i need is a bit of code to count the number of open workbooks.

    Incase you are curius why i am finishing my dad's work off, being as he uses the command to open the previous workbook if there are any other workbooks open when it starts it will crash part the way threw running, so i am adding a message which pops up if there are other workbooks open and then close them if i can be bothered.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Counting Open work books

    Quote Originally Posted by machinist
    Very simple i am sure all i need is a bit of code to count the number of open workbooks.

    Incase you are curius why i am finishing my dad's work off, being as he uses the command to open the previous workbook if there are any other workbooks open when it starts it will crash part the way threw running, so i am adding a message which pops up if there are other workbooks open and then close them if i can be bothered.
    Have you tried this:

    VB Code:
    1. Workbooks.Count

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Counting Open work books

    Its best to use GetObject and CreateObject since Workbooks.Count will only detect workbooks that are opened inside a particular Excel instance.
    If they are all opened from the start menu and not Excel's Open or New menu then it will not work.

    VB Code:
    1. Private Sub Form_Load()
    2.     On Error Resume Next
    3.     Dim oApp As Object
    4.     Set oApp = GetObject
    5.     If Not oApp Is Nothing Then
    6.         'Attached to previous instance
    7.     Else
    8.         Set oApp = CreateObject("Excel.Application")
    9.     End If
    10. End Sub
    May have an error since I typed it into the reply box and not VB.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Counting Open work books

    Quote Originally Posted by RobDog888
    Its best to use GetObject and CreateObject since Workbooks.Count will only detect workbooks that are opened inside a particular Excel instance.
    If they are all opened from the start menu and not Excel's Open or New menu then it will not work.

    VB Code:
    1. Private Sub Form_Load()
    2.     On Error Resume Next
    3.     Dim oApp As Object
    4.     Set oApp = GetObject
    5.     If Not oApp Is Nothing Then
    6.         'Attached to previous instance
    7.     Else
    8.         Set oApp = CreateObject("Excel.Application")
    9.     End If
    10. End Sub
    May have an error since I typed it into the reply box and not VB.

    Thanks for that tip, I'm just starting to try and learn VBA so just about everything is new too me and I love it

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Counting Open work books

    No prob.

    The Workbooks collection is a bit misleading. It all depends on how the user opens the workbooks. When I first started with it I thought it contained
    a collection of all instanciated workbooks, but not. It would have made things a bit easier if it was.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width