Save Word Document from C# AddIN refuses to work

K

karenina

Hello there.

I try to develop an Office plugin (in C#, .NET Framework 1.0) which is
supposed to manage some documents. I am trying to save the custom
properties for an word document and then send this document via email.
I though the steps are:

- let the user select the document (therefore open the document)
- let the user write the properties (using some simple Windows Form
that allows the user to enter the name and the value of the properties)
- update the custom props on the document
- save the document
- close the document
- get the byte[] and use them however I want.

The problem is the Save method is not working. I found this out by
re-opening the document after it was closed. I can find all the new
props on my Document object, but after I close and load the document,
there are no props there.

Can someone please help me?

I am using C#, .NET framework 1.0 (the application is really big and
there is no time to transfer it to 1.1 or 2.0 at this moment, so I have
to use framework 1.0), Microsoft Office 2003 and I downloaded the
latest PIAs from Microsoft. I alse copied the dlls from the PIAs to my
own project and used that ones.

Thanks a lot for your time.
 
F

Frenchy

Hello

I'm using custom properties and document variables in my .NET1 addin for
Word2003.

Both are saved correclty

this.appWord.ActiveDocument.Save();

Maybe there's a problem with the update of your custom props

Here's my code, hope It will help you;

public void SetDocCustomProp(string propName, string propValue)
{

try
{
object customPropertiesObject =
this.appWord.ActiveDocument.CustomDocumentProperties;
Type propertyType = customPropertiesObject.GetType();

object[] oArgs =
{propName,false,MsoDocProperties.msoPropertyTypeString,propValue};
try
{
propertyType.InvokeMember("Add",System.Reflection.BindingFlags.Default
| System.Reflection.BindingFlags.InvokeMethod, null,customPropertiesObject,
oArgs );
}
catch(Exception)
{
propertyType.InvokeMember("Item",BindingFlags.Default |
BindingFlags.SetProperty,null,customPropertiesObject,new object[]
{propName,propValue} );
}

}
catch (Exception e)
{
Clipboard.SetDataObject(e.ToString(),true);
MessageBox.Show(e.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

}
 
G

Gary McGill

I think I've seen this problem. It was a while back, but I think I remember
that if you open a Word doc, modify the value of an existing Custom Document
Property, and then call Save, nothing happens (your changes are not saved).
I think this is because Word doesn't consider the document to be changed
(which is clearly a bug). As I recall, I had to make a change to the body of
the document so that Word knows it has to be saved. You might be able to set
the Document.Saved propery to False instead.

Gary
 
K

karenina

Thanks for your reply. I believe there is indeed a bug since I set the
properties and the Saved property of the Word Document is true.
I managed to solve this by setting this property to false (which tells
Word Application that it should indeed save the changes).
After that it worked fine.

Thanks a lot,

Roxana.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top