Click here to Skip to main content
13,592,846 members
Rate this:
Please Sign up or sign in to vote.
Please need your help regarding this code. I am writing 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 , my code is alright and suppose to work as i don't get any compile error as well. but is not doing what i ask it to do as i don't get any result. Please guys , will really appreciate it if some one will help me with an alternative 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)
                    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)
                  Object Companyprop = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Company" });
                    Type typeCompanyprop = Companyprop.GetType();
                               BindingFlags.Default |
                               null, Companyprop,
                               new object[] { companyprop, companyvalue });

                 catch (COMException)


Posted 13-Apr-12 16:46pm
Nelek 14-Apr-12 8:58am
Did you try debugging? Where is it failing? Any error messages?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

For C# DOCX file format, you can easily add / edit / iterate over document properties using this C# / VB.NET Word library.

Here is a sample C# code:
// Use the component in free mode.

// Create new empty document.
var document = new DocumentModel();

// Add various built-in document properties.
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Title, "My document title");
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Subject, "My document subject");
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Author, "John Doe");
document.DocumentProperties.BuiltIn.Add(BuiltInDocumentProperty.Keywords, "keyword1, keyword2, keyword3");

// Save the document to a file.
document.Save("Document.docx", SaveOptions.DocxDefault);

// Load a document from the file.
document = DocumentModel.Load("Document.docx", LoadOptions.DocxDefault);

// Iterate over built-in document properties and write them to Console.
foreach (var builtInProperty in document.DocumentProperties.BuiltIn)
	Console.WriteLine("{0}: {1}", builtInProperty.Key, builtInProperty.Value);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I don't know if you now need the solution but I do this thing.
My code didn't work in your version too.
I remove external method "removeproperty" and place all code to "execute_Click" and situation became Ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.180618.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100