Resolve path of Excel - c#

Actually I intended to ask the following question:
For an executable that lies in a
directory defined in %PATH%, how can
I find out in which of these directory
it is found?
because I need to run Excel from C# using Process.Run(...), and just indicating "Excel" works fine: Windows seems to know where to find it. However I need to provide the appropriate working directory, too.
But in the meantime I found out that Microsoft Office is not even in the %PATH%, Windows apparently resolves it in another way.
So how could I use or mimic this functionality from Windows to get the directory containing excel?
Update: Besides the suggestion of Ruel, is there a straighter way to achieve this? I know that Windows is able to resolve the path in some way, so I'd like to reuse this functionality if possible.
Any idea?

Fetch it from the registry: HKLM\SOFTWARE\Microsoft\Office\(VERSION)\Excel\InstallRoot for the VERSION it can be 10.0 11.0 etc. You can detect it by checking if there's a subkey Excel in it.

Related

Share code between office applications

I have written two addins , 1 for excel and 1 for word. However these addins have a lot of duplicates: Database handling, file handling,array handling which I would like to update 1 place instead of two.
We do have access to sharepoint, and could get access to visual studio. The thing is that people like to use file explorer and find the correct word or excel file, then open it then press a button inside the application which then should do things with the active document.
This is why we haven't written it as a .Net application yet, because that requires that people browse for the file inside the .NET application uless I am mistaken.
Is it possible to make an Addin which works both excel and word, or a dll? AnAnother important thing is that it should be easy to roll out a new version to the user, like stored on a network drive or similar.
Yes it is possible
The Hard Way
You can create a .Net DLL and call it from VBA. In visual studio a lot of people use Unmannaged Exports by Robert Giesecke to create DLLs that don't need to be registered (that way the DLL can be shipped with your document, and as long as it can be found you can use it).
Alternatively you might be able to do it manually as shown here by Hans Passant.
The Easy Way
Once the DLL is created you can declare it in a VBA module the same way you declare any other DLL for Late Binding and then call it from your code.
OR if you're happy to create the DLL and add it as a reference (possibly less portable) you can make it COM visible and register it for COM Interop in Visual Studio; this is probably the easiest way to go because you can then use Early Binding.
This is a walk through that might help: http://www.geeksengine.com/article/create-dll.html
But if you want to store the DLL on a network drive, well it might be that you really want to look at doing it the 'hard way', in which case look here: https://stackoverflow.com/a/5934745/3451115 and here: https://msdn.microsoft.com/en-us/library/bb687915.aspx

Programatically get path of open Excel/Word-document

Is there any way to get the path of documents currently open in Microsoft Word and Microsoft Excel?
I know how I can fetch the PID and the window handle for the running instance of Excel or Word, but I have no idea how to get the path of the open document. Is it even possible?
I am thinking that an API would be nice, but I doubt it even exists. Perhaps some kind of plugin that sends information to my application is a possibility? (But if any other way is possible, I'd like to avoid having to install plugins everywhere.)
I would prefer to do it using C#, but anything that can be compiled into an executable is alright.
And just in case someone thinks this sounds fishy, it is not to be used for anything remotely illegal. It is for a data collection system at work.
There is an API called Office Interop... it is basically a COM based interface for MS Office... and can be used from C# .
For Excel you can get the current file including path via Application.ActiveWorkbook.FullName.
For Word you can get the current file including path via Application.ActiveDocument.FullName.

.NET Junction Directories and DirectInfo issues

I am trying to access information under C:\Users\Public\Documents\ which displays as C:\Users\Public\Public Documents\ on explorer. Is there a way to be able to access the DirectoryInfo of a directory under the junction using the pathname displayed in explorer in .NET C#?
When/how are you expecting to get a C:\users\public\public documents path?
If a user goes to copy/paste from Explorer, the C:\users\public\documents path will be copied into the clipboard. Going through a SaveFileDialog also produces the valid path.
If you need a way to access Windows 7 libraries, you can give the Windows API Code Pack a go. You can then access "Public Documents as KnownFolders.PublicDocuments, for example. From what I can see (I've never used it before), there's still no way to reliably turn C:\users\public\public documents into a DirectoryInfo, but you might have better luck digging through the docs.

Error 1001 An error occured in sending the command to the application

COM Excel AddIn, C#, VS2008
The error happens occasionally when I install/uninstall my AddIn.
sometimes I see Error 1001 the specified file can not be found
Anyone know what causes these and how to fix? thanks
I use windows installer
http://msdn.microsoft.com/en-us/library/2w2fhwzz%28v=VS.90%29.aspx says if use [TARGETDIR], it should be like "[TARGETDIR]\" or "[TARGETDIR] ". I simply use /filepath = "[TARGETDIR]myinstallfile" in CustomActionData
What I do not understand is it works almost all time and fails occasionally
Also even if I change this to including space or backslash, I can't tell if that fixes issue since the issue does not happen every time. Anyone has experience? thanks
I found this and it fixes the issue though I am not sure I ever use DDE in my program
http://sympmarc.com/2010/02/04/microsoft-excel-error-there-was-a-problem-sending-the-command-to-the-program/
Then I found this http://www.opendylan.org/documentation/opendylan/interop2/inte_278.htm
It talks about COM Server
so I went to cmd, type in "Excel.exe /RegServer", then the error disappears.
I am not really not sure if this solution works for all cases.
In fact, I am concerned that I miss sth in installer.
Here is a Microsoft Support page related to an issue which looks quite similar to yours. So for me it looks like a bug in Excel rather than in your installer.
The article is quite large, but it boils down to making sure that:
your Excel app is not running with elevated rights
advanced setting "Ignore other applications that use Dynamic Data Exchange (DDE)" is unchecked
Other than that you might try to repair Excel installation or follow the advice given in this thread of ASP.NET forums to fix the registry for Excel installation.
I hope it helps someone facing similar issues.
If you get this type of error when uninstalling a VS setup project MSI, then the most likely reason is that TARGETDIR is not preserved between the install and the uninstall, therefore it has no value, and attempts to use it in an uninstall custom action will result in failure to find the file. The easiest solution (apart from always installing to known locations such as common files etc) is to save TARGETDIR to the registry and retrieve it later. In the VS IDE you can create a registry item with the value [TARGETDIR] to have it resolved at install time.

Pass multiple files / folders from windows explorer to external application

Hi does anyone know how to get windows explorer to pass multiple files / folders through to an external app (c#) referenced in the registry?
I am current able to act upon a single file / folder using the %1 syntax but not sure how to get explorer to pass through multiple items.
Does anyone know how to do this?
When you select multiple files in Explorer, your shell context menu extension's IShellExtInit::Initialize method will be called and pdtobj contains the selection.
Note writing managed shell extension is not supported.
I don't think this is possible.
When you open multiple files using Explorer, it will launch a separate copy of your program for file. I don't think it's possible to override this behavior.
EDIT: I forgot about shell extensions. This is possible.
To work around this, you could make the subsequent copies communicate with the first one, then exit. Detailed instructions for this are beyond the scope of this answer.
In order to do this reliably you would need to write a shell extension, most likely a sendto implementation.
I haven't written one since vb6 but you can find what looks to be a good managed example here
Or you could use a freeware utility

Categories

Resources