|
-
May 25th, 2005, 03:19 PM
#1
Thread Starter
Member
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.
-
May 25th, 2005, 03:27 PM
#2
Re: Counting Open work books
 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:
Cheers,
RyanJ
-
May 25th, 2005, 03:34 PM
#3
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:
Private Sub Form_Load()
On Error Resume Next
Dim oApp As Object
Set oApp = GetObject
If Not oApp Is Nothing Then
'Attached to previous instance
Else
Set oApp = CreateObject("Excel.Application")
End If
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 25th, 2005, 03:39 PM
#4
Re: Counting Open work books
 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:
Private Sub Form_Load()
On Error Resume Next
Dim oApp As Object
Set oApp = GetObject
If Not oApp Is Nothing Then
'Attached to previous instance
Else
Set oApp = CreateObject("Excel.Application")
End If
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
-
May 25th, 2005, 03:46 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|