Consulting

Results 1 to 13 of 13

Thread: Solved: Auto_Open/Auto_Close

  1. #1
    VBAX Newbie
    Joined
    May 2007
    Posts
    5
    Location

    Solved: Auto_Open/Auto_Close

    I'm having problems understanding why the code that I've got doesn't run at either open or close.

    This code is attached to the sheet.
    ____________________________________
    [VBA]
    Option Explicit
    Dim oCmdPopup As CommandBarPopup
    Dim oCmdButton As CommandBarButton
    Dim oCmdCtrl As CommandBarControl

    Sub Auto_Open()
    Set oCmdPopup = CommandBars("Worksheet Menu Bar").Controls("File")
    Set oCmdButton = oCmdPopup.Controls.Add(Before:=15)
    With oCmdButton
    .Caption = "Change Column Widths"
    .FaceId = 59
    .OnAction = "ShowForm"
    End With
    End Sub

    Sub ShowForm()
    frmChangeWidth.Show
    End Sub
    Sub Auto_Close()
    Set oCmdPopup = CommandBars("Worksheet Menu Bar").Controls("File")
    For Each oCmdCtrl In oCmdPopup.Controls
    If oCmdCtrl.Caption = "Change Column Widths" Then
    oCmdCtrl.Delete
    Exit For
    End If
    Next oCmdCtrl
    End Sub
    [/VBA]
    _______________________________________
    If I run the code from the view code window it does what it's supposed to do. This is exactly the code the instructor gave us for this project. What am I missing?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try putting your code the the workbook open event and place it in the thisworkbook module:
    [VBA]Private Sub Workbook_Open()
    'your code
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If it's a lesson you may wish to try auto open but it still needs to be in the thisworkbook module...can you post the instructions?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Newbie
    Joined
    May 2007
    Posts
    5
    Location
    Can you create Sub Auto_Open() within Private Sub Workbook_Open()?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Where did you store this code, which code module?

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    What version of Excel are you using...if it's a later version you are recommended to use Workbook_Open instead of Auto_Open. From the Help File version 2003
    RunAutoMacros Method
    Runs the Auto_Open, Auto_Close, Auto_Activate, or Auto_Deactivate macro attached to the workbook. This method is included for backward compatibility. For new Visual Basic code, you should use the Open, Close, Activate and Deactivate events instead of these macros.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lucas
    What version of Excel are you using...if it's a later version you are recommended to use Workbook_Open instead of Auto_Open. From the Help File version 2003
    But it should still work, except in AUtomation.

  8. #8
    VBAX Newbie
    Joined
    May 2007
    Posts
    5
    Location
    Office 2002

  9. #9
    VBAX Newbie
    Joined
    May 2007
    Posts
    5
    Location
    It in the worksheet not the workbook

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I agree Bob but I get this error no matter how I try to run it: object variable or with block variable not set
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Auto macros need to be in a standard code module, not the worksheet module
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mpaneitz
    It in the worksheet not the workbook
    You can only put worksheet event procedures, and procxedures called by such procedures, in a worksheet code module. For anything els, Excel will look in a standard code module, so that is where you must store them.

  13. #13
    VBAX Newbie
    Joined
    May 2007
    Posts
    5
    Location
    So there were 2 issues:
    1) the code I posted originally has to be in a module, not workbook or worksheet.
    2) I was trying to put the menu item befor #15 in the file menu. That might have been too low. I set it at before #4 and it worked consistently.

    Thank you all for the messages.

Posting Permissions

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