Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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 http://support.microsoft.com/kb/303296 , 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.


C#
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)
                {

                }


}
Posted
Comments
Nelek 14-Apr-12 8:58am    
Did you try debugging? Where is it failing? Any error messages?

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:
C#
// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// 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);
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900