get relative path of where running macro is saved

get relative path of where running macro is saved

stefanveurink68AXD
Advocate Advocate
2,447 Views
3 Replies
Message 1 of 4

get relative path of where running macro is saved

stefanveurink68AXD
Advocate
Advocate

I got and .dll from which a macro is called. in this macros there's paths to certain blocks, textfonts and stuff to load them. 

 

Now, everything was working perfectly fine, but since this whole corona thing there's more need to run everything from different locations (people working at home and stuff), so I only have the path relative to where the macro is saved. 

For an excel this is not an problem cause the macro is part of an workbook. However, this is not the case within Autocad. And so I can't find an answer anywhere. 

 

So, is there a way to get the place where the macro is saved from within the macro itself?

 

 

0 Likes
Accepted solutions (1)
2,448 Views
3 Replies
Replies (3)
Message 2 of 4

stefanveurink68AXD
Advocate
Advocate

I still haven't found any solution or workaround on this problem. 

 

Any ideas on how I could make this work?

0 Likes
Message 3 of 4

norman.yuan
Mentor
Mentor
Accepted solution

Since you mention "... dll from which a macro is called..." without describing what the DLL is and how it calls the macro (I assume that by "macro" you do mean a public Sub that is defined in a VBA module), that makes people hesitate to respond. 

 

Let me simplify the question: your user could copy your VBA file (*.dvb) along with some other resources (block files, font files...) to a folder of his/her computer at his/her choice. You want the code in your VBA project to be able to know where the folder is during execution.

 

Firstly, I would write code in the VBA project to always search current support paths for required resources, and ask user to place your VBA project file and its required resources in one of the support paths. You might also ask user to make sure the VBA file in in a trusted location. This approach is more in line with AutoCAD's setup traditions.

 

If you really want to know the actual path of VBA project file that user could be randomly placed by the code inside the VBA project, you can get it by using MS VBA Extensibility 5.x. Firstly, add reference to Microsoft Visual Basic for Application Extensibility 5.3, the you can write following Function:

 

Public Function GetVbaProjectFileName(vbProjName As String) As String
    Dim vb As VBIDE.VBE
    Set vb = ThisDrawing.Application.VBE
    
    Dim proj As VBIDE.VBProject
    Dim fName As String
    On Error Resume Next
    For Each proj In vb.VBProjects
        If UCase(proj.Name) = "MYVBAPROJECT" Then
            
            fName = proj.FileName
            If Err.Number <> 0 Then
                fName = "The VBA Project has not been saved"
            End If
            GetVbaProjectFileName = fName
            Exit Function
        End If
    Next
    GetVbaProjectFileName = ""
    
End Function

 

Here is the sample code to use the Function to get VBA project's file name, as long as you know the project's name (of course you know, because you build the project and you name the project whatever you want) and it is loaded (of course it is loaded, because your are running it):

 

Public Sub Test()
    Dim vbProjFile As String
    vbProjFile = GetVbaProjectFileName("MyVBAProject")
    If Len(vbProjFile) > 0 Then
        MsgBox vbProjFile
    Else
        MsgBox "Cannot find VBA Project: not loaded."
    End If
    
End Sub

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 4

stefanveurink68AXD
Advocate
Advocate

Thanks!

 

I understand what your saying, but don't want to ask user this everytime. And prefer to not even ask it once, can't exactly explain why. 

 

Anyways, your code and extensibility did give me the right way to do it, and it works perfectly. Knew it couldn't be hard but didn't have an entrance point, once I saw your post it was easy as cake.

 

So again, thanks a lot! 

0 Likes