Consulting

Results 1 to 10 of 10

Thread: SOLVED: Disable Custom Buttons when no Worksheet is Loaded

  1. #1
    VBAX Regular
    Joined
    Apr 2008
    Posts
    7
    Location

    SOLVED: Disable Custom Buttons when no Worksheet is Loaded

    Hi All,

    This is my first post on here so I hope I explain myself well.

    What i am looking to do is to disable my custom buttons in my custom toolbar when there is no worksheet open and as soon as ANY worksheet is loaded enable the buttons.

    I have the code that enables and disables the buttons but cant find any trigger points to call this code.

    This is the code i have for the two actions

     
        For Each Item In Application.CommandBars.Item("NameofToolbar").Controls
               Item.Enabled = False
        Next Item
    &
     
        For Each Item In Application.CommandBars.Item("NameofToolbar").Controls
               Item.Enabled = True
        Next Item
    Any help would be appreciated.

    Many thanks

    Mark
    Last edited by lockma; 04-18-2008 at 03:36 AM. Reason: Question answered

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I don't understand, how do you not have any worksheet open?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Apr 2008
    Posts
    7
    Location
    If you open excel then a blank worksheet is created. if you then go to file>close the blank worksheek is closed but excel is still running.

    It's when Excel is running but no sheets are open that I want my buttons to be disabled - Like most of the inbuilt toolbar buttons.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    How about a workbook open event that opens a messagebox that asks if you want to delete the sheets and if so then run your code based on a 0 count of worksheets?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Regular
    Joined
    Apr 2008
    Posts
    7
    Location
    I'm not sure you understand what i am asking - I dont want any messageboxes etc. All i want to do is for Excel to disable my custom buttons (ie grey them out) when there are no worksheets open.

    Thanks for the replies though

  6. #6
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    I tried to find a way of doing this and never succeeded. Best I could do is as Steve suggested. I'm not sure there is an event you could tie this to...and if there is, I'd love to know it!

  7. #7
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Hi mark,
    Quote Originally Posted by lockma
    What i am looking to do is to disable my custom buttons in my custom toolbar when there is no worksheet open and as soon as ANY worksheet is loaded enable the buttons
    Replace 'worksheet' with 'workbook' and the sentence will be perfect.

    Quote Originally Posted by lockma
    If you open excel then a blank worksheet is created. if you then go to file>close the blank worksheek is closed but excel is still running
    File>Close refers to workbooks, not to worksheets. Worksheets are either added or deleted within a workbook, never File>Closed.

    Quote Originally Posted by lucas
    I don't understand, how do you not have any worksheet open?
    See? The misleading is obvious... You misled us (me too) into thinking Worksheetwise when you actually meant everything Workbookwise...

    Anyway, I think I have an answer for what you want.

    Since you want your custom buttons to be in the toolbar even when no files are open, then they should be placed there from code within the 'Personal.xls' file (which is in the OFFICExx\XLSTART folder). This file is automatically opened every time you start Excel (and stays open until you quit Excel).

    Now. More code must go into this file, in order to check the number of workbooks that are open. When only one workbook is found open (i.e. the Personal.xls) that would mean that the user has closed all other files. In that case you would disable your buttons.

    One way to do it is the following.
    In the ThisWorkbook code module of the Personal.xls, declare a new variable
    [vba]Dim WithEvents xlApp As Application[/vba]
    In the Workbook_Open() event of the Personal.xls, you instantiate this variable
    [vba]Private Sub Workbook_Open()
    If xlApp Is Nothing Then
    Set xlApp = Application
    End If
    'Create buttons in toolbar
    End Sub
    [/vba]
    Then you add the code you want in these events:
    [vba]Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
    If Application.Workbooks.Count >= 2 then
    'enable buttons
    End If
    End Sub[/vba]

    [vba]Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
    If Application.Workbooks.Count = 2 then
    'disable buttons
    End If
    End Sub[/vba]
    You quit Excel saving the Personal.xls when asked to, and then you reopen it.

    What happens is, as you close the open workbooks, the _WorkbookBeforeClose event fires and the code checks how many files are open at the moment.
    When the last visible file is being closed, the Workbooks.count will be 2 (Personal.xls is still open in the background plus this file which hasn't closed yet) and after this file is permanently closed there will be no more visible files open and the buttons will be disabled.
    Did that answer to your question in any way?

    Edit: Added the xlApp_WorkbookOpen event and did some slight rephrasing of the closing sentences.
    Last edited by tstav; 04-17-2008 at 11:54 AM.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  8. #8
    VBAX Regular
    Joined
    Apr 2008
    Posts
    7
    Location
    Workbook, worksheet - what's in a name eh?

    Probably a heck of a lot so i apologise whole heartily for the confusion - i did say on my first post i hope i explain myself well and i guess i failed miserably!!

    Thanks for the comprehensive reply btw and it looks like it will do the job perfectly. When i am in work tomorrow i will give it a go but it looks like what i want - just hope it works

    Will let you all know tomorrow and mark it as solved if it does the trick.

    Have a good night (or afternoon or morning depending on where you all are)

  9. #9
    VBAX Regular
    Joined
    Apr 2008
    Posts
    7
    Location
    Thanks tstav - worked a treat apart from i had to add in a another event for a new workbook to trigger the enable function as well or the buttons weren't enables if i created a new blank workbook.

    Complete code in the end was:-

    [vba]
    Dim WithEvents xlApp As Application

    Private Sub Workbook_Open()
    If xlApp Is Nothing Then
    Set xlApp = Application
    End If
    'Create buttons in toolbar
    End Sub

    Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)
    If Application.Workbooks.Count >= 2 Then
    For Each Item In Application.CommandBars.Item("Pitman").Controls
    Item.Enabled = True
    Next Item
    End If
    End Sub

    Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
    If Application.Workbooks.Count = 2 Then
    'disable buttons
    For Each Item In Application.CommandBars.Item("Pitman").Controls
    Item.Enabled = False
    Next Item
    End If
    End Sub

    Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
    If Application.Workbooks.Count >= 2 Then
    For Each Item In Application.CommandBars.Item("Pitman").Controls
    Item.Enabled = True
    Next Item
    End If
    End Sub
    [/vba]

    Thanks again for all of your help

  10. #10
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Good job, lockma!

    I'm glad I helped.

    Best regards,
    tstav
    He didn't know it was impossible, so he did it. (Jean Cocteau)

Posting Permissions

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