|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Core;
using System.Reflection;
namespace WordAddIn1
{
public class CustomDocumentPropertyManager
{
public string GetPropertyValue(string propertyName,ref Microsoft.Office.Interop.Word.Document Doc)
{
string propertyValue = String.Empty;
Type validatedType = null;
object oDocCustomProps = null;
Type typeDocCustomProps = null;
string prop = String.Empty;
object property = null;
try
{
oDocCustomProps = Doc.CustomDocumentProperties;
typeDocCustomProps = oDocCustomProps.GetType();
prop = propertyName; //property to extract
property = typeDocCustomProps.InvokeMember("Item",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.GetProperty,
null, oDocCustomProps, new object[] { prop });
validatedType = property.GetType();
//to check the value of custom document property
propertyValue = validatedType.InvokeMember("Value",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.GetProperty,
null,
property,
new object[] { }).ToString();
}
catch (System.Reflection.TargetInvocationException ex)
{
propertyValue = String.Empty;
}
catch (Exception ex)
{
propertyValue = String.Empty;
}
return propertyValue;
}
public bool AddCustomDocumentProperty(string propertyName, string propertyValue,ref Microsoft.Office.Interop.Word.Document Doc)
{
bool success = false;
Type validatedType = null;
object oDocCustomProps = null;
Type typeDocCustomProps = null;
string prop = String.Empty;
object property = null;
try
{
oDocCustomProps = Doc.CustomDocumentProperties;
typeDocCustomProps = oDocCustomProps.GetType();
prop = propertyName;
property = typeDocCustomProps.InvokeMember("Item",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.GetProperty,
null, oDocCustomProps, new object[] { prop });
}
catch (System.Reflection.TargetInvocationException ex)
{
oDocCustomProps = Doc.CustomDocumentProperties;
typeDocCustomProps = oDocCustomProps.GetType();
string strIndex = propertyName;
string strValue = propertyValue;
object[] oArgs = {strIndex,false,
MsoDocProperties.msoPropertyTypeString,
strValue};
typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
BindingFlags.InvokeMethod, null,
oDocCustomProps, oArgs);
success = true;
}
catch (Exception ex)
{
success = false;
}
return success;
}
public bool ChangePropertyValue(string propertyName, string propertyValue,ref Microsoft.Office.Interop.Word.Document Doc)
{
bool success = false;
object oDocCustomProps = null;
Type typeDocCustomProps = null;
string prop = String.Empty;
object property = null;
try
{
oDocCustomProps = Doc.CustomDocumentProperties;
typeDocCustomProps = oDocCustomProps.GetType();
prop = propertyName;
property = typeDocCustomProps.InvokeMember("Item",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.GetProperty,
null, oDocCustomProps, new object[] { prop });
typeDocCustomProps.InvokeMember("Value",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.SetProperty,
null,
property,
new object[] { propertyValue });
success = true;
}
catch (Exception ex)
{
success = false;
}
return success;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.