Get path to XLL

1,661 views
Skip to first unread message

Bogey

unread,
Aug 10, 2011, 4:29:33 AM8/10/11
to Excel-DNA
Hi,

I'm stuck on a little issue. I need to know the path of where the .xll
being loaded is located in order to be able to load database and
licensing-files from my addon which are located in a subfolder.

Seems like a simple problem, but so far I couldn't come up with a
solution. If you have any ideas right away, just ignore the rest of
this message because I'll just try to explain what I have tried so far
(none of it really worked, though).



First approach:

Just calling

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:
\\","");

in my addin. But that only returns the path to the Windows GAC-folder.




Second approach:

Using a function

public static string path() {
xcelInterop.Application app =
(ExcelInterop.Application)ExcelDnaUtil.Application;
return app.ActiveWorkbook.Path;
}

but obviously, there are two problems with that:
1. Only returns the workbook path, not the .xll-path, so if multiple
workbooks in various directories are using the same .xll it'd be
useless
2. Of course only works after the workbook which is supposed to use
the .xll is loaded. This is my main problem because unfortunately, I
have to load the .xll before the workbook, so that's not an option.




Third approach:

Since I'm using a seperate .exe file to start up excel, register the
addin and load the .xls my second idea was to simply have my .exe file
register an environment variable:

string path =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:
\\","");
Environment.SetEnvironmentVariable("MyAddin_Path", path,
EnvironmentVariableTarget.User)

and load my addin etc. after registering that env. variable so that in
my addin I could simply call GetEnvironmentVariable("MyAddin_Path").

For a reason I don't really get this only works "delayed", though,
meaning that right after setting the environment variable, it is not
updated when trying to get it in my Excel-Addin. When I close
everything and restart using my .exe, THEN it's available "up to
date". So basically that's whats happening:

.exe sets env. variable to "1". load addin. addin gets env variable =>
value undefined.
close everything, reload .exe:
.exe sets env. variable to "2". load addin. addin gets env. variable
=> value "1".
close everything, reload .exe
.exe sets env. variable to "3". load addin. addin gets env. variable
=> value "2".
etc.

I suppose that's however some sort of general C# issue and not a
particular ExcelDNA-problem.



So thats all I could think of and none of it really achieved what I
needed. Is there any way to get the path to my ".xll" you could think
of? Thanks!

Govert van Drimmelen

unread,
Aug 10, 2011, 7:06:34 PM8/10/11
to Excel-DNA
Hi,

You can try:

string myName = (string)XlCall.Excel(XlCall.xlGetName);

-Govert

On Aug 10, 10:29 am, Bogey <f.meh...@googlemail.com> wrote:
> Hi,
>
> I'm stuck on a little issue. I need to know the path of where the .xll
> being loaded is located in order to be able to load database and
> licensing-files from my addon which are located in a subfolder.
>
> Seems like a simple problem, but so far I couldn't come up with a
> solution. If you have any ideas right away, just ignore the rest of
> this message because I'll just try to explain what I have tried so far
> (none of it really worked, though).
>
> First approach:
>
> Just calling
>
> System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAsse mbly().CodeBase).Replace("file:
> \\","");
>
> in my addin. But that only returns the path to the Windows GAC-folder.
>
> Second approach:
>
> Using a function
>
> public static string path() {
> xcelInterop.Application app =
> (ExcelInterop.Application)ExcelDnaUtil.Application;
> return app.ActiveWorkbook.Path;
>
> }
>
> but obviously, there are two problems with that:
> 1. Only returns the workbook path, not the .xll-path, so if multiple
> workbooks in various directories are using the same .xll it'd be
> useless
> 2. Of course only works after the workbook which is supposed to use
> the .xll is loaded. This is my main problem because unfortunately, I
> have to load the .xll before the workbook, so that's not an option.
>
> Third approach:
>
> Since I'm using a seperate .exe file to start up excel, register the
> addin and load the .xls my second idea was to simply have my .exe file
> register an environment variable:
>
> string path =
> System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAsse mbly().CodeBase).Replace("file:

Bogey

unread,
Aug 11, 2011, 5:50:15 AM8/11/11
to Excel-DNA
Thanks Govert, that did it!! Now that was easy..and I used to try to
find a workaround for a week or so.. ;)
> > of? Thanks!- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Andrew Kittredge

unread,
Jul 27, 2017, 3:23:23 PM7/27/17
to Excel-DNA, f.me...@googlemail.com
I think ExcelDnaUtil.XllPath is a better way to get the path, as discuessed @ https://github.com/Excel-DNA/ExcelDna/issues/13.
Reply all
Reply to author
Forward
0 new messages