Author Topic: Make your macro know its place.  (Read 3091 times)

0 Members and 1 Guest are viewing this topic.

havano

  • Guest
Make your macro know its place.
« on: November 17, 2006, 05:58:23 PM »
Suppose you have built a macro that uses some INI and DATA files for input or output, located in the same directory. You have hard-coded the path into your DVB file, so that your macro knows where to look for these files.

The end-users decide to make the macro available on a network server drive. But the client PC's use different drive letters for this drive. Also, the IT department changes the directory name for the application. Result: the macro can't find the INI and DATA files.

Here's a solution:

Code: [Select]
Public Sub GetMyLocation()
Dim strDVBname As String, strDVBpath As String
strDVBname = VBE.ActiveVBProject.Name & "."
strDVBpath = VBE.ActiveVBProject.BuildFileName

strDVBpath = left(strDVBpath, InStr(strDVBpath, strDVBname) - 1)
Debug.Print strDVBpath

'    e.g. strDVBpath will be "AnyDrive:\AnyDir\AnySubdir\"
'    so your macro can access data files in this directory
'    no matter where the directory is created.
End Sub

I hope this is of some use for some.
« Last Edit: November 23, 2006, 12:15:38 AM by havano »

Dnereb

  • Guest
Re: Make your macro know its location.
« Reply #1 on: November 19, 2006, 02:42:05 PM »
How about something like this to do the same:

Public Function GetMyLocation()
GetMyLocation =   Replace(VBE.ActiveVBProject.BuildFileName ,VBE.ActiveVBProject.Name, "")
End Function

havano

  • Guest
Re: Make your macro know its location.
« Reply #2 on: November 19, 2006, 02:59:16 PM »
Beautiful, Dnereb!

One small improvement:
Code: [Select]
Public Function GetMyLocation()
GetMyLocation = Replace(VBE.ActiveVBProject.BuildFileName ,VBE.ActiveVBProject.Name & ".DLL", "")
End Function

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Make your macro know its location.
« Reply #3 on: November 19, 2006, 04:48:34 PM »
How about something like this to do the same:

Public Function GetMyLocation()
GetMyLocation =   Replace(VBE.ActiveVBProject.BuildFileName ,VBE.ActiveVBProject.Name, "")
End Function
I think this code can get wrong result if VBE.ActiveVBProject.BuildFileName has subdirectory equal VBE.ActiveVBProject.Name
Be carefully!

havano

  • Guest
Re: Make your macro know its location.
« Reply #4 on: November 19, 2006, 04:56:23 PM »
Hence the addition of "." in my first example and ".DLL" in my reply to Dnereb's posting. The latter being necessairy anyway to make the function work properly  :angel:.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Make your macro know its location.
« Reply #5 on: November 19, 2006, 05:01:40 PM »
Hence the addition of "." in my first example and ".DLL" in my reply to Dnereb's posting. The latter being necessairy anyway to make the function work properly  :angel:.
But ".DLL" can be also in subdirectory path! For example: C:\TEST.DLL\MyOwnDirectory\Test.DLL\TEST.DLL
« Last Edit: November 19, 2006, 05:06:59 PM by Alexander Rivilis »

havano

  • Guest
Re: Make your macro know its location.
« Reply #6 on: November 19, 2006, 05:13:16 PM »
OK, Alexander. Any suggestions?
(A 100% fail-safe method would of course be locating the rightmost backslash in the string, and trim the string from there.)

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Make your macro know its location.
« Reply #7 on: November 19, 2006, 05:21:39 PM »
OK, Alexander. Any suggestions?
(A 100% fail-safe method would of course be locating the rightmost backslash in the string, and trim the string from there.)
You absolutely right! I do not write with VBA (VB, etc) If VBA has function which can substitute only one (!!!) right occurrence of substring in string - this function can be useful. In other case you have to do that function yourself! :)

havano

  • Guest
Re: Make your macro know its place.
« Reply #8 on: November 22, 2006, 11:25:51 PM »
Code: [Select]
Public Sub KnowMyPlace()
Dim MyDir As String
MyDir = StrReverse(VBE.ActiveVBProject.BuildFileName)
MyDir = StrReverse(Right(MyDir, Len(MyDir) - InStr(MyDir, "\") + 1))
End Sub