Avi888

Hi

I don't have a lot of knowlege in COM so the problem that I encountered was very strange to me.
I'm trying to create a button in my Word appliction through a Shard Addin project in C#.

When I'll press this button a certain logic that I wrote will be executed.


I encountered two problems.

1) I can't create the button in my Word application but only in my outlook and I don't know why.


I followed exactly the code that's written here:
http://support.microsoft.com/default.aspx scid=kb;en-us;302901
But the button was not created.
I Added the right assemblies for Word and Office and the CheckBox for "Register for COM interop" was checked.
I tried to install all of office components again but it still didn't worked
Then I tried to check whether the Addin workd at all so I wrote a messageBox in the OnStartupComplete to see if the MessageBox shows-up.
The MessageBox - MessageBox.Show("Hi") - only shows up in Power Point and Outlook. Not in Word or Excel.

2) After the button will be clicked I want to retrieve the path of the current Word Document that's running.
Since I couldn't do it with Word, I tried to do it with "Power Point" just to see that it works.
I created a string variable that holds the path of the current object according to the code that I found here in a similar question.
I checked to see if the file path was retrieved by setting one of the MessageBox parameters with the string value of the path:

Type CurrentApplicationType = applicationObject.GetType();

object ActiveDocument = CurrentApplicationType.InvokeMember("ActivePresentation", System.Reflection.BindingFlags.GetProperty, null, applicationObject, null);

ActiveFilePath = (string)ActiveDocument.GetType().InvokeMember("FullName", System.Reflection.BindingFlags.GetProperty,null, ActiveDocument, null);

MessageBox.Show("ActiveFilePath", ActiveFilePath);

The result was that the MessageBox doesn't show up at all.

It does shoes up when I write a regular MessageBox - MessageBox.Show("Hi").




Re: Visual Studio Tools for Office A strange problem in retrieving a file path and creating a button in Word

X4U

Hello Avi,

we had a similar thread here - maybe there's the information you need.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1418719&SiteID=1

As i can remember I had to create a COM Shim Wizard to get it working within Excel.

Hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office A strange problem in retrieving a file path and creating a button in Word

Avi888

Hi X4U

Before I posted my question I already searched for similar threads and found it.

I tried to use some of the code there but so far I didn't successed.

First, like I wrote, I don't have a lot of expireance with COM so I don't konw what is a COM shim.

According to some of the answers in that thread I managed to get the name of the application - PowerPoint - but that's it.

When I try to get the ActivePresentation - the activeDocument object - I'm thrown to the catch block.

Besides, like I wrote, I want to do it in Word - create a button in Word and get the current document that's running - not in powerpoint :-)

I have office 2003

Windows xp Home

Why can't I get "activeDocument" and then the "documentPath" And why does the Word and Excel applications don't even connect to the Addin Does it has any connection to the registry keys.

Code Snippet

try

{

Type applicationType = applicationObject.GetType();

string ApplicationName = (string)applicationType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, applicationObject, null);

object activeDocument = applicationType.InvokeMember("ActivePresentation", System.Reflection.BindingFlags.GetProperty , null, applicationObject, null);

string documentPath = (string)activeDocument.GetType().InvokeMember("FullName", System.Reflection.BindingFlags.GetProperty, null, activeDocument, null);

MessageBox.Show("Successed", ApplicationName);

}

catch

{

MessageBox.Show("failed");

}






Re: Visual Studio Tools for Office A strange problem in retrieving a file path and creating a button in Word

Avi888

Good news!!

I finally managed to to it!!!

thanks!






Re: Visual Studio Tools for Office A strange problem in retrieving a file path and creating a button in Word

X4U

Hello Avi,

here you cfan find the ComShimWizard wich is used to create a COM AddIN wich loads your .Net AddIn.

http://msdn2.microsoft.com/en-us/library/aa537166(office.11).aspx

So I read that you get it working - what was the solution

Greets, Helmut