Reputation: 522
Please need your help regarding this code. I am wrting a code in c# that can will clear a word document built in property, and also replace it with a provided replacement where provided. Base on the example i found online in microsft support website http://support.microsoft.com/kb/303296 , my code is alright and suppose to work as i dont get any compile error as well. but is not doing what i ask it to do as i dont get any result. Please guys , will really appreciate it if some one will help me with an ulternative or point out my error so that the weeks i spent will ruin in vain. Thanks you as you help. Below is my code.
private void execute_Click(object sender, EventArgs e)
{
Word.Application wapp;
Word.Document dc = new Word.Document() ;
Object bSaveChanges = false;
string chosen_file = "";
chosen_file = openFD.FileName;
textBox1.Text = (chosen_file);
var filter = Path.GetExtension(chosen_file);
object Filename = chosen_file.ToString();
if (filter == ".doc" || filter == ".docx")
{
wapp = new Word.Application();
wapp.Visible = true;
docword = wapp.Documents.Add(ref Filename, ref missing, ref missing, ref missing);
object _BuiltInProperties = docword.BuiltInDocumentProperties;
Type typeDocBuiltInProps = _BuiltInProperties.GetType();
removeproperty(_BuiltInProperties, typeDocBuiltInProps);// pass parameter
docword.Close(ref bSaveChanges, ref missing, ref missing);
wapp.Quit(ref bSaveChanges, ref missing, ref missing);
}
}
private void removeproperty(object _BuiltInProperties, Type typeDocBuiltInProps)
{
string subjectprop = "Subject";
string subjectValue = "";
string companyprop = "Company";
string companyvalue = txtcompany.Text;
if (clearsubject.Checked == true)
{
try
{
Object Subjectprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Subject" });
Type typeSubjectprop = Subjectprop.GetType();
typeSubjectprop.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, Subjectprop, new object[] { subjectprop, subjectValue });
}
catch (COMException)
{
}
}
if (resetcompany.Checked == true)
{
try
{
Object Companyprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Company" });
Type typeCompanyprop = Companyprop.GetType();
typeCompanyprop.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null, Companyprop,
new object[] { companyprop, companyvalue });
}
catch (COMException)
{
}
}
Upvotes: 1
Views: 3826
Reputation: 189
1) Add reference to Microsoft.CSharp.
2) Get BuiltInDocumentProperties:
private void LoadMetadata(Document Doc) {
this.Title.Text = Doc.BuiltInDocumentProperties["Title"].Value;
this.Subject.Text = Doc.BuiltInDocumentProperties["Subject"].Value;
this.Category.Text = Doc.BuiltInDocumentProperties["Category"].Value;
this.Keywords.Text = Doc.BuiltInDocumentProperties["Keywords"].Value;
this.Author.Text = Doc.BuiltInDocumentProperties["Author"].Value;
this.Comments.Text = Doc.BuiltInDocumentProperties["Comments"].Value;
}
3) Set new values:
private void SaveMetadata(Document Doc) {
Doc.BuiltInDocumentProperties["Title"].Value = this.Title.Text;
Doc.BuiltInDocumentProperties["Subject"].Value = this.Subject.Text;
Doc.BuiltInDocumentProperties["Category"].Value = this.Category.Text;
Doc.BuiltInDocumentProperties["Keywords"].Value = this.Keywords.Text;
Doc.BuiltInDocumentProperties["Author"].Value = this.Author.Text;
Doc.BuiltInDocumentProperties["Comments"].Value = this.Comments.Text;
}
Upvotes: 1
Reputation: 8514
As found here, you can change word properties by name like so:
void SetWordDocumentPropertyValue(Word.Document document, string propertyName, string propertyValue)
{
object builtInProperties = document.BuiltInDocumentProperties;
Type builtInPropertiesType = builtInProperties.GetType();
object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
Type propertyType = property.GetType();
propertyType.InvokeMember("Value", BindingFlags.SetProperty, null, property, new object[] { propertyValue });
document.UpdateSummaryProperties();
document.Save();
}
Property names are like "Author"
, "Last Author"
, "Title"
, etc...
Upvotes: 2