Click here to Skip to main content
Rate this: bad
good
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 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.
 

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 13-Apr-12 16:46pm
Comments
Nelek at 14-Apr-12 8:58am
   
Did you try debugging? Where is it failing? Any error messages?
Rate this: bad
good
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.
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);
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 389
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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