app path

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

  1. perry

    perry Guest

    In "straight" VB there is a construct called "app path", a variable which
    obviously returns the path of its application.
    Does such a thing exist in VBA? I cant seem to find it. So far I have been
    fudging it with ...
    "Path = Application.VBE.activevbproject.FileName", but this is not reliable,
    particularly if other VBA routines get loaded into the same drawing.
    thanks
     
    perry, Oct 12, 2004
    #1
  2. perry

    Dan Guest

    From the Help Files I found the following:

    Sub Example_Path()
    ' This example returns the path of the drawing and application.

    ' If the drawing has not been saved, the path of the
    ' document will be blank.
    Dim docPath As String
    docPath = ThisDrawing.Path
    MsgBox "The Path of the current drawing is " & docPath, , "Path Example"

    Dim appPath As String
    appPath = ThisDrawing.Application.Path
    MsgBox "The Path of the current appication is " & appPath, , "Path
    Example"

    End Sub
     
    Dan, Oct 13, 2004
    #2
  3. perry

    thenov Guest

    I found this in a thread and it seems to work fine for me. However the issue of multiple VBA modules may still be a problem for you.
    ------------------------------------------------------------------------
    The solution is in adding a reference to Microsoft visual basic for applications Extensibility How do you do that?
    In the Visual Basic for Applications Editor:

    "Tools" menu
    Sub menu "References"
    In the dialog box look for:
    Microsoft visual basic for applications Extensibility <version number here>
    Check it
    OK button <click>

    Now try this:

    Public Sub LocateME()
    Dim objIDE As VBIDE.VBE
    Set objIDE = Application.VBE
    MsgBox objIDE.VBProjects.Item(1).BuildFileName
    MsgBox objIDE.ActiveVBProject.BuildFileName
    End Sub
     
    thenov, Oct 13, 2004
    #3
  4. perry

    perry Guest

    Yes, I saw that. It gives the path to AutoCad
     
    perry, Oct 13, 2004
    #4
  5. perry

    perry Guest

    So far this has worked if there are multiple VBA apps loaded, but
    unfortunately it Always gives a bogus result when Acad first starts up. Any
    drawings loaded afterwards have a correct path
    thanks

    --
    Perry Leets
    Inovec Optimization and Control Systems
    Eugene, Oregon
    issue of multiple VBA modules may still be a problem for you.applications Extensibility How do you do that?
    FileName property will CRASH AutoCAD.
     
    perry, Oct 13, 2004
    #5
  6. perry

    perry Guest

    Weird. I was trying to figure out why my fuction (listed below) always gave
    a bogus path when Acad first started but was fine afterwards.
    so I put a bad statement (dooda) in the function to cause it to break into
    the VBAIDE so I could examine the VBIDE variable and when I did so
    it looked just fine!
    So is it that maybe VBA isnt fully initialized when I call this function??

    code snippet---
    Private Function app_path()
    Dim objIDE As VBIDE.VBE
    Set objIDE = Application.VBE
    Dim tPath As String
    Dim pos As Integer

    tPath = objIDE.ActiveVBProject.BuildFileName
    pos = InStrRev(tPath, "\")
    app_path = Left(tPath, pos)
    dooda = dooda
    End Function
    end code snippet---
     
    perry, Oct 13, 2004
    #6
  7. perry

    Kevin Terry Guest

    I've used a similar function for a long time with good results, and haven't
    seen the bogus path problem you're reporting. What is bogus about it?

    One suggestion: use Stop instead of dooda = dooda
    One more suggestion: use a global variable with the project name as
    comparison to make sure the app_path function doesn't return the path of
    some other project that is loaded

    my function:
    'sProgram is a global constant with the name of the project
    Public Function App_Path() As String

    Dim objIDE As VBIDE.VBE
    Dim i As Long

    Set objIDE = Application.VBE

    For i = 1 To objIDE.VBProjects.Count
    If UCase(objIDE.VBProjects(i).Name) = UCase(sProgram) Then
    App_Path = objIDE.VBProjects(i).BuildFileName
    End If
    Next i

    'see if .dll was substituted for .dvb & fix it (this happens in 5.0)
    If InStr(1, UCase(App_Path), ".DLL") > 0 Then _
    App_Path = Left(App_Path, InStr(1, UCase(App_Path), ".DLL") - 1) & _
    ".dvb"

    Set objIDE = Nothing

    End Function

    hth,
    Kevin
     
    Kevin Terry, Oct 14, 2004
    #7
  8. perry

    perry Guest

    By bogus, I meant that it was returning the path of a different app, not the
    one I wanted.
    (suggestion taken, I forgot about that "stop" thing ;)
    Your method of recursing though the VBprojects looks like it would do the
    trick, Ill implement
    that asap and see how it does.
    Thanks
     
    perry, Oct 14, 2004
    #8
  9. perry

    perry Guest

    Just one tiny problem with that function, I dont know about your machine but
    on mine (objIDE.VBProjects(i).name) ALWAYS returns "ACADapplication",
    regardless of what project its looking at, dunno why.
    So I made a slight modification to your function and it works like a charm.
    By the way, this also solved my menu problem I posted on a few days ago.
    Scroll up about 20 or 30 posts to see. Yippie!!
    Note also I changed the function to return only the app path, not including
    app name.
    Thanks for the tip.

    CODE snippet ------------
    'sProgram is a global constant with the name of the project
    Public Function App_Path() As String

    Dim objIDE As VBIDE.VBE
    Dim i As Long
    Dim tPath As String
    Dim pos As Integer

    Set objIDE = Application.VBE
    For i = 1 To objIDE.VBProjects.Count
    If (InStr(UCase(objIDE.VBProjects(i).BuildFileName), UCase(sProgram)))
    Then
    tPath = objIDE.VBProjects(i).BuildFileName
    End If
    Next i
    pos = InStrRev(tPath, "\")
    App_Path = Left(tPath, pos)
    Set objIDE = Nothing

    End Function

    CODE snippet ends ------------
     
    perry, Oct 14, 2004
    #9
  10. perry

    perry Guest

    Gotta renig on one thing, it DIDNT fix my menu problem as far as acad2002 is
    concerned :(
     
    perry, Oct 14, 2004
    #10
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.