Author Topic: VBA Application path in AutoCAD  (Read 7087 times)

0 Members and 1 Guest are viewing this topic.

Patch61

  • Guest
VBA Application path in AutoCAD
« on: July 27, 2014, 02:06:13 AM »
I came up with these routines for getting the path to the current project's .dvb file and also to extract just the path from a full path and filename, for AutoCAD VBA. I split this into separate functions because I thought that the GetPathFromFilename function could be useful in other places. I hope others find these useful.

Public Function App_Path() As String
   'Works like app.path in VB
   Dim str As String
   str = ThisDrawing.Application.VBE.ActiveVBProject.Filename
   App_Path = GetPathFromFilename(str)
End Function

Public Function GetPathFromFilename(sFile) As String
   Dim arr() As String
   arr = Split(sFile, "\", -1, vbTextCompare)
   GetPathFromFilename = Left(sFile, Len(sFile) - Len(arr(UBound(arr))))
End Function

caddcop

  • Guest
Re: VBA Application path in AutoCAD
« Reply #1 on: July 30, 2014, 11:22:26 AM »
FYI
There is something in VBA that stores the path internally and I believe is stores the path when created. After that, no matter where you move the file to, it still reports the original path.
We have a number of DOT Clients that provide a family of VBA applications and some attempt to use this method to find support files relative to the application file path. These all fail to work and have required us to develop another approach for these to function. Generally, we end up using INI files or environment variables.

Patch61

  • Guest
Re: VBA Application path in AutoCAD
« Reply #2 on: August 07, 2014, 12:39:56 AM »
I'm not sure what you are trying to say. Are you saying the code I posted does not work? It works perfectly fine on my system... and also on my client's systems. Win7 Pro 64-bit, AutoCAD 2014 & 2015 VBA 7.1 64-bit. I just tested it by moving the DVB file to a completely newly created folder on a different physical HD and my function returned the correct path to the new location of the DVB file.

« Last Edit: August 07, 2014, 12:44:53 AM by Patch61 »

caddcop

  • Guest
Re: VBA Application path in AutoCAD
« Reply #3 on: August 18, 2014, 12:26:24 PM »
What I am saying is that the VBA Project path is often stored in the VBA application when the project is created. If you move the application to a different location, it may not update that path in the VBA Project. We have seen this in Microsoft product VBA projects and non-Microsoft product VBA projects.
When it comes to AutoCAD, its VBA implementation may not have this issue.
What I have seen is that the number of available AutoCAD VBA examples and freewaretools on the web is a fraction of the number of lisp routines. So my experience with AutoCAD's VBA is very limited. And given the problems  have encountered in other programs when we try using the VBA project path, we generally avoid it since there seems to be no easy solution.
If this is not a problem with AutoCAD, that's great. Just be forewarned, it may not be the case in other software that supports VBA.