Can I specify a location in the user's home directory for a shared library in VBA (in Office for Mac)?

I am currently using VBA code similar to the one below to indicate the location of the shared library to use when transferring (transferring a string) from an Office application to a desktop application. VBA code / macros must exist in the add-in (.ppa).

Private Declare Sub sharedLibPassString CDecl Lib "/Users/myUserName/Library/Application Support/myCompanyName/MySharedLib.dylib" Alias "PassString" (ByVal aString As String) 

In the code from VBA Macro, I can do the following.

 Call sharedLibPassString(myString) 

Communication works for me, but I would like to replace the /Users/myUserName/ part with the user's current home directory. Typically on Mac you specify ~/Library/Application Support/... , but the ~/ syntax does not work, creating a "File not found" runtime error.

I found that using the following environment variable method gives me the place I need ~/ :

 Environ("HOME") 

However, I see no way to do this part of the CDecl Lib , since, as far as I can tell, Environ is evaluated at runtime.

Is it possible to specify the location of the shared library in the user's home directory ( ~/ ) in VBA?


Here are a few notes about my environment / approach:

  • I use a Mac, although I believe that if there is a solution, it will look like a PC.
  • I don’t think it matters, but the Office application I use is PowerPoint (2011).
  • The reason I'm trying to access the area inside the application support directory, and not the default for shared libraries, is because I would like the Desktop application to host the shared library in a place without an installer and without requiring user or administrator rights. If there is a better solution or location to accomplish the same task, it is also very useful.
+6
source share
2 answers

Sorry for the long answer, I just wanted to make sure I explained it pretty well.

From this page (Anatomy of the declaration statement) we read that

The lib keyword indicates which DLL contains this function. Note that the dll name is contained in the string in the ad expression. (highlighted by me)

From an experiment, VBE scolds me if I try to give anything other than a string constant.

The only work I know of is overwriting the string constant at runtime.

Here is an example of how this can be done: let them say that your delaration statement is located in Module1 in your current project and that you intentionally wrote an ad in this format at the top of your module:

 Private Declare Sub sharedLibPassString CDecl Lib _ "/Users/myUserName/Library/Application Support/myCompanyName/MySharedLib.dylib" _ Alias "PassString" (ByVal aString As String) 

You can access this module through code through this (for this, permissions for VBA in the trust center specified in the "Developer macro settings" section are required):

 Dim myModule set myModule = ActivePresentation.VBProject.VBComponents("Module1").CodeModule 

Once you have purchased CodeModule, you can directly replace the second line:

 myModule.ReplaceLine 2, Environ("HOME") & " _" 

Mission accomplished!

If you do this, you will need to update the path before trying to call your declared under. There must be a performance gap that allows VBA to recognize changes. In addition, you cannot change the code in break mode.

Now, if I did this, I would like me to replace the correct line so as not to break the code. You can check the contents of the second string by calling this myModule.Lines(2,1) , which will return the string value of the string.

However, here is a more robust solution that will find the correct string and then replace it (assuming myModule is already defined as above):

 Dim SL As Long, EL As Long, SC As Long, EC As Long Dim Found As Boolean SL = 1 ' Start on line 1 SC = 1 ' Start on Column 1 EL = 99999 ' Search until the 99999th line EC = 999 ' Search until the 999th column With myModule 'If found, the correct line will be be placed in the variable SL 'Broke search string into two pieces so that I won't accidentally find it. Found = .Find("/Users/myUserName/Library/Application Support/myCompanyName/" & _ "MySharedLib.dylib", SL, SC, EL, EC, True, False, False) If Found = True Then 'Replace the line with the line you want, second paramater should be a string of the value. .ReplaceLine SL, Environ("HOME") & " _" End If End With 
+2
source

I don't have a Mac, so this is an incomplete answer, and I don't know if that will help, but these are a couple of ideas.

I know on Windows, VB does not load an external library until you try to call the function declared with it, and if you specify only the file name in the declare statement, it will use the system path to search.As soon as I did the same thing you do by loading the library from a dynamic path, specifying only the file name, and then making a system API call to set the current working directory to the library directory before loading it. It may also change the PATH environment variable.

Second idea: you can hard-code the path to the file name in the / tmp directory; then automatically copy the desired library to this place before downloading it. Make sure the file is used by another process, but this is only an error if it is a different version of the file that you want.

+2
source

All Articles