VBA-created Toolbar disappears

Discussion in 'AutoCAD' started by D Hartley, Apr 29, 2004.

  1. D Hartley

    D Hartley Guest

    I have a vba routine to create a macro-assigned toolbar . It creates the
    toolbar just fine, and the buttons work just fine.
    However, if I close AutoCAD, when I open AutoCAD again, the toolbar has
    disappeared.

    Can you see a probem with this bit of code?

    Dim objToolbar As AcadToolbar
    Set objToolbar= ThisDrawing.Application.MenuGroups("ACAD").Toolbars.Add("My
    Toolbar")

    Thanks a lot
    David
     
    D Hartley, Apr 29, 2004
    #1
  2. D Hartley

    Kevin Terry Guest

    I used to have that problem too. Got around it by making a custom menu file
    (also thru code) and loading it as follows:

    Public Const sCompany As String = "Jaypro Sports"

    Dim sFile As String
    sFile = Support_Path & sCo & ".mns"
    lFile = FreeFile
    Open sFile For Output As lFile
    Print #lFile, sMenu
    Close #lFile
    'sMenu has the actual text of the menu

    Set acGroup = ThisDrawing.Application.MenuGroups
    Set acMenu = acGroup.Load(sFile)
    Set acToolBar = acMenu.Toolbars.item(sCompany)

    'apply icons to buttons
    For i = 1 To UBound(sButtons, 2)
    Set acButton = acToolBar.item(sButtons(2, i))
    acButton.SetBitmaps sButtons(3, i), sButtons(3, i)
    Next i

    'apply icons to flyouts
    For i = 1 To UBound(sFlyouts, 1)
    'first the flyout main button
    Set acToolBar = acMenu.Toolbars.item("zFly-" & sFlyouts(i, 2, 1))
    For j = 1 To UBound(sFlyouts, 3)
    'get out of loop at first empty string = last button
    If sFlyouts(i, 1, j) = "" Then Exit For
    'then the individual buttons
    Set acButton = acToolBar.item(sFlyouts(i, 2, j))
    acButton.SetBitmaps sFlyouts(i, 3, j), sFlyouts(i, 3, j)
    Next
    Next

    acMenu.Save acMenuFileCompiled

    HTH,
    Kevin
     
    Kevin Terry, Apr 29, 2004
    #2
  3. You need to save your menugroup otherwise the toolbar is dropped:

    ThisDrawing.Application.MenuGroups.Item(0). _
    SaveAs "MyMenu.mnc", acMenuFileCompiled
     
    Mike Tuersley, Apr 29, 2004
    #3
  4. D Hartley

    D Hartley Guest

    Kevin,

    For a while, the following approach worked after I created the toolbar

    ThisDrawing.Application.MenuGroups.Item(0).SaveAs
    "MyMenu.mnc",acMenuFileCompiled
    Sometimes the toolbar persists and sometimes not



    As far as your routine,
    I'm following this except for a couple of things, and I have a feeling
    these are questions to be scoffed at by an actual programmer, but
    regarding

    Print #lFile, sMenu
    How do you get the actual text into sMenu- do you Print it line by line?

    Also
    How do you know what menu out of the available menus the user is using?

    Thanks,
    David
     
    D Hartley, Apr 30, 2004
    #4
  5. D Hartley

    Kevin Terry Guest

    How do you get the actual text into sMenu- do you Print it line by line?
    Yes - ever since I wrote it initially I've been meaning to put it into a
    spreadsheet driven format or something easier to maintain from the code
    side, but it's been a back shelf project...

    Without overloading you (i hope) with miles of meaningless code, here's a
    starter:
    'file header
    sMenu = "//" & vbCrLf & "// " & sCompany & vbCrLf & "//" & vbCrLf & _
    "// " & sDiaTitle & " " & sVersion & " Menu" & vbCrLf & "//
    " & sVersion & _
    vbCrLf & "//" & vbCrLf & vbCrLf
    'title section
    sMenu = sMenu & "***MENUGROUP=" & UCase(sCo) & vbCrLf & vbCrLf
    'toolbars section
    sMenu = sMenu & "***TOOLBARS" & vbCrLf & vbCrLf
    'main toolbar
    sMenu = sMenu & "**TB_" & UCase(sCo) & vbCrLf & "ID_" & LCase(sCo) & _
    " [_Toolbar(""" & sCompany & """, " _
    & "_Floating, _Show, 240, 160, 1)]" & vbCrLf

    'sButtons is an array populated from some spaghetti code of routines and
    bitmap names, etc.
    'there are a lot of better ways to do this...
    'add buttons
    For i = 1 To UBound(sButtons, 2)
    sMenu = sMenu & "ID_" & LCase(Part_From_Path(sButtons(3, i))) _
    & Space(16 - Len(Part_From_Path(sButtons(3, i))) - 2) _
    & "[_" & sButtons(0, i) & "("""
    etc, etc
    I don't assume anything - each time this runs it's running from scratch, in
    other words it's creating a toolbar from nothing either after uninstalling
    and reinstalling my app or for a brand new install. Since the user may have
    toolbars loaded from multiple menus, this was the cleanest way I could think
    of.

    Kevin
     
    Kevin Terry, Apr 30, 2004
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.