2002 vs 2004 menu problem

Discussion in 'AutoCAD' started by perry, Oct 8, 2004.

  1. perry

    perry Guest

    Can anyone tell me why this chunk of code works fine on Acad2004 but gives
    an "automation catastropy" type error in 2002?
    In the debugger, the menuname (path) looks fine. This is weird...

    Public Sub LoadMenu(path As String)
    'Load the Tblocker drop down menu if it is not already present.
    'Path is the full path and file name of the menu
    'Called by -> "main-Tbinit"
    'Attach to AutoCAD
    Dim ObjAcad As Object
    Dim ObjDoc As Object
    Set ObjAcad = GetObject(, "AutoCAD.Application")
    Set ObjDoc = ObjAcad.ActiveDocument
    Dim ObjMenuGroup As AcadMenuGroup
    Set ObjMenuGroup = ObjAcad.MenuGroups.item(0)
    Dim ObjMenuGroups As AcadMenuGroups
    Set ObjMenuGroups = ObjAcad.MenuGroups
    Dim ObjPopup As AcadPopupMenu
    Dim MenuExists As Boolean

    For Each ObjPopup In ObjAcad.MenuBar
    If ObjPopup.NameNoMnemonic = "Title Block" Then
    MenuExists = True
    Exit For
    End If
    Next ObjPopup

    If Not MenuExists Then
    On Error Resume Next ' trap any load errors
    ObjMenuGroups.Load path
    If Err.Number <> 0 Then
    MsgBox "Unable to load Tblocker menu"
    Err.Clear
    GoTo done
    End If
    Set ObjMenuGroup = ObjMenuGroups.item("TBlocker")
    Set ObjPopup = ObjMenuGroup.menus.item(0)
    ObjPopup.InsertInMenuBar (ObjAcad.MenuBar.Count + 1)
    End If
    done:
    Set ObjDoc = Nothing
    Set ObjAcad = Nothing
    End Sub
     
    perry, Oct 8, 2004
    #1
  2. perry

    perry Guest

    by the way, path evaluates to "C:\Program
    Files\graphics\Tblocker\tblocker.mnu"
     
    perry, Oct 8, 2004
    #2
  3. First off you can't use GetObject(, "AutoCAD.Application") if both are
    installed. You need to use Application.16 and Application.15.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Oct 9, 2004
    #3
  4. Another weird thing, are you trying to late bind? If you are, then you
    should be diming everything as object - which you are getting away with
    because you haven't declared option explicit.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Oct 9, 2004
    #4
  5. perry

    perry Guest

    Sorry I took so long getting back, kinda tied up this weekend ;)
    Thanks for the tip Mike, Im pretty good at missing the obvious
    (application.15). I think thats one of the biggest pains of developing apps
    is having
    to maintain multiple versions where the only difference is they are linking
    to different libraries.
    Anyways, I made 2 versions of this app, one using (application.15) for
    acad2002 and one using (application.16) for acad2004.
    That didnt seem to fix the problem though, my code is still croaking at the
    "menugroups.load" statement even though its getting a valid menu name.
    Pretty annoying, just cant figure it.
    Oh and by the way, I do have "Option explicit" at the top of EVERY module I
    make, just to play it safe. It just didnt make it into my skillful cut&paste
    operation. I cant imagine why I would want this code to late bind???
     
    perry, Oct 11, 2004
    #5
  6. I cant imagine why I would want this code to late bind???
    Then why are you? This is your code snippet:

    Dim ObjAcad As Object
    Dim ObjDoc As Object

    That IS late binding. If you are early binding, then it should be:

    Dim oAcad As AcadApplication
    Dim oDoc As AcadDocument

    Normally you'd late bind to have your app work with multiple versions of
    AutoCAD.


    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Oct 11, 2004
    #6
  7. perry

    perry Guest

    Well, I never claimed to be a VB guru. Is that THE way to differentiate
    late/early binding in VBA?
    Late binding in this case didnt seem to help with my menu problem. How does
    late binding in this way
    help code work on different versions of Acad if we still have to reference
    specific libraries?
    Thanks
     
    perry, Oct 11, 2004
    #7
  8. perry

    perry Guest

    Ok, forget the early/late question, I called upon my typical minimal effort,
    and with a bit of help from google found a document on MSDN which cleared
    this up nicely.
    Im of the impression now that early binding is probably better, since Im
    going to have to maintain 2 versions of this anyway might as well.
    Will alter code accordingly, but I have a feeling it may not fix the menu
    problem. I'll let you know shortly.
    thanks
     
    perry, Oct 11, 2004
    #8
  9. perry

    perry Guest

    Well I went through the code checking all the references and set them for
    earling binding. By the way doesnt that make the
    Set ObjAcad = GetObject(, "AutoCAD.Application.15") reference moot? that
    construct itself created an error until I replaced it with
    Set ObjAcad = ThisDrawing.Application.
    As I suspected though, none of this fixed my menu problem, any other ideas
    on this??
    Thanks
     
    perry, Oct 11, 2004
    #9
  10. Okay, Perry, I haven't got time at the moment to look at your code but I
    will in the morning. Till then, have you looked at/compared to the VBA
    Sample in the Samples\VBAIDE folder?

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Oct 11, 2004
    #10
  11. perry

    perry Guest

    Thanks for the time Mike, here is my latest code for that function...

    Option Explicit

    Public Sub LoadMenu(path As String)
    'Load the Tblocker drop down menu if it is not already present.
    'Path is the full path and file name of the menu
    'Called by -> "main-Tbinit"
    'Attach to AutoCAD
    Dim ObjAcad As AcadApplication
    Dim ObjDoc As AcadDocument
    Set ObjAcad = ThisDrawing.Application
    Set ObjDoc = ObjAcad.ActiveDocument
    Dim ObjMenuGroup As AcadMenuGroup
    Set ObjMenuGroup = ObjAcad.MenuGroups.item(0)
    Dim ObjMenuGroups As AcadMenuGroups
    Set ObjMenuGroups = ObjAcad.MenuGroups
    Dim ObjPopup As AcadPopupMenu
    Dim MenuExists As Boolean

    For Each ObjPopup In ObjAcad.MenuBar
    If ObjPopup.NameNoMnemonic = "Title Block" Then
    MenuExists = True
    Exit For
    End If
    Next ObjPopup

    If Not MenuExists Then
    On Error Resume Next ' trap any load errors
    'autocad 2002 bombs here, even though "path" is valid ("C:\Program
    Files\graphics\Tblocker\tblocker.mnu")
    ObjMenuGroups.Load path
    If Err.Number <> 0 Then
    MsgBox "Unable to load " & path & " : " & Err.Description
    Err.Clear
    GoTo done
    End If
    Set ObjMenuGroup = ObjMenuGroups.item("TBlocker")
    Set ObjPopup = ObjMenuGroup.menus.item(0)
    ObjPopup.InsertInMenuBar (ObjAcad.MenuBar.Count + 1)
    End If
    done:
    Set ObjDoc = Nothing
    Set ObjAcad = Nothing
    End Sub
     
    perry, Oct 11, 2004
    #11
  12. I tried your code in 2002 and it worked fine. It must be something within
    your menu file ????

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Oct 12, 2004
    #12
  13. perry

    perry Guest

    Thanks for the help Mike ;)
    Ill try it with a different file and see what happens, strange though this
    same menu file works in 2004.
    Is there something different between 2002 and 2004 menus?
     
    perry, Oct 12, 2004
    #13
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.