D
DarrylR
I have discovered (much to my disappointment) that in order to access Word
document properties and methods from a Windows Forms application, I have to
use reflection instead of calling the methods directly. For example, when I
need to access the Word.Document.CustomDocumentProperties collection from
VSTO (VBA would be similar), I can use the following syntax:
Microsoft.Office.Interop.Word.ApplicationClass objWdClass =
new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document objDoc =
objWdClass.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref
oMissing);
string strPropertyName = "ProductId";
object objValue = "theproductid";
Microsoft.Office.Core.DocumentProperties objDocProps =
(Microsoft.Office.Core.DocumentProperties)objDoc.CustomDocumentProperties;
objDocProps[strPropertyName].Value = objValue;
However, the aforementioned code throws a 'cast is invalid' exception when
you attempt to cast the object returned by objDoc.CustomDocumentProperties
to type DocumentProperties. As a result, I was forced to use the following
syntax:
object oDocCustomProps = objDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
object oCustomProp = typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocCustomProps,
new object[] {strPropertyName});
typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null, oDocCustomProps,
new object[] {strPropertyName, objValue} );
I realize that the objects need to be marshalled across processes. But is it
really necessary to use reflection? Is there anything that would make this
easier? Any information/advice would be greatly appreciated.
Thanks,
Darryl R.
document properties and methods from a Windows Forms application, I have to
use reflection instead of calling the methods directly. For example, when I
need to access the Word.Document.CustomDocumentProperties collection from
VSTO (VBA would be similar), I can use the following syntax:
Microsoft.Office.Interop.Word.ApplicationClass objWdClass =
new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document objDoc =
objWdClass.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref
oMissing);
string strPropertyName = "ProductId";
object objValue = "theproductid";
Microsoft.Office.Core.DocumentProperties objDocProps =
(Microsoft.Office.Core.DocumentProperties)objDoc.CustomDocumentProperties;
objDocProps[strPropertyName].Value = objValue;
However, the aforementioned code throws a 'cast is invalid' exception when
you attempt to cast the object returned by objDoc.CustomDocumentProperties
to type DocumentProperties. As a result, I was forced to use the following
syntax:
object oDocCustomProps = objDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
object oCustomProp = typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, oDocCustomProps,
new object[] {strPropertyName});
typeDocCustomProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null, oDocCustomProps,
new object[] {strPropertyName, objValue} );
I realize that the objects need to be marshalled across processes. But is it
really necessary to use reflection? Is there anything that would make this
easier? Any information/advice would be greatly appreciated.
Thanks,
Darryl R.