locked
How to change the properties from word programatically RRS feed

  • Question

  • Regards. 
    Is there any way to change the properties of word with programming. Net, visual basic?.

    I've seen something like

        Private Wapp As Word.Application
        Private docWord As Word.Document
        Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Dim _BuiltInProperties As Object = docWord.BuiltInDocumentProperties
            If Not _BuiltInProperties Is Nothing Then
                _BuiltInProperties("Title").Value = txtTitle.Text.Trim()
                _BuiltInProperties("Subject").Value = txtSubject.Text.Trim()
                _BuiltInProperties("Author").Value = txtAuthor.Text.Trim()
                _BuiltInProperties("Manager").Value = txtManager.Text.Trim()
                _BuiltInProperties("Company").Value = txtCompany.Text.Trim()
                _BuiltInProperties("Category").Value = txtCategory.Text.Trim()
                _BuiltInProperties("Keywords").Value = txtKeyWords.Text.Trim()
                _BuiltInProperties("Comments").Value = txtComment.Text.Trim()
            End If
            MessageBox.Show("Summery has been updated successfully")
            If Not docWord Is Nothing Then
                docWord.Close()
            End If
            If Not Wapp Is Nothing Then
                Wapp.Quit()
            End If
    
        End Sub
    But that only works with properties there, you can not change such "creation date", "last print date", etc.. 
    I need to change these properties. 
    Also I think for this to work should have the word api in my server, ie to install word on the server, and do not know if this will be possible. 
    What would be the best froma do? 

    Friday, March 21, 2014 5:46 PM

Answers

All replies

  • Hello Augusto,

    Some built-in document properties are read only. So, you are not allowed to change them using the Word object model. See for details How do I read/write a document's BuiltIn properties? .

    As a workaround, you can try to use Open XML SDK. Be aware the open XML files can be supported. This will not work for binary files (*.doc). See the following forum threads for more information:

    how to programmatically access the builtin properties of an open xml worddoc file 

    how to programatically access built-in properties of open xml word doc?

    Retrieving Core Document Properties

    • Proposed as answer by Marvin_Guo Monday, March 24, 2014 9:59 AM
    • Unproposed as answer by Augusto C Wednesday, March 26, 2014 4:29 PM
    • Proposed as answer by Jeffrey_Chen_ Wednesday, April 2, 2014 2:14 AM
    • Marked as answer by Marvin_Guo Wednesday, April 2, 2014 11:48 AM
    Saturday, March 22, 2014 9:59 AM
  • hi. 
    thanks for answering 
    And if I have to change the properties of a file .doc. 
    How can I get it?
    • Edited by Augusto C Tuesday, March 25, 2014 2:16 PM
    Tuesday, March 25, 2014 2:16 PM
  • Hello Augusto,

    You can find a lot of commercial components for editing binary Office files programmatically (for example, aspose). Or just automate the Office applications.

    Tuesday, March 25, 2014 8:27 PM
  • hi, thanks for answering. 
    About? Aspose you will have a link?. 
    What do you mean with "Or just automate the Office applications".? 
    Something like a macro?

    thanks

    regards

    • Edited by Augusto C Tuesday, March 25, 2014 9:01 PM
    Tuesday, March 25, 2014 9:00 PM
  • You can try to google for aspose. I am sure, it will be the first record from the list.

    >> What do you mean with "Or just automate the Office applications".? 
    Something like a macro?

    This is what you are doing at the moment in your add-in.

    Tuesday, March 25, 2014 9:25 PM
  • Hi,

    As Eugene said, you can use OpenXML SDK to retrieve and modify Core Properties from Word Documents.

    If you didn’t know about OpenXML SDK, you can get start to following link:

    Welcome to the Open XML SDK 2.0 for Microsoft Office

    http://msdn.microsoft.com/en-us/library/bb448854(v=office.14).aspx

    The build-in properties is saved in /docProps/Core.xml, here is a sample code to modify Subject property in word document.

    using System.Xml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
                        WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(@"D:\1.docx", true);
                        CoreFilePropertiesPart corePart = wordprocessingDocument.CoreFilePropertiesPart;
                XmlDocument xmlProperties = new XmlDocument();
                xmlProperties.Load(corePart.GetStream());
                        //Access Subject property
                string DC = @http://purl.org/dc/elements/1.1/;
                        //Get the Subject property
                var Subject = xmlProperties.GetElementsByTagName("subject",DC).Item(0).InnerText;
                if (Subject != null)
                    message += "Subject : " + Subject + "\n";
                //modify the Subject property
                xmlProperties.GetElementsByTagName("subject", DC).Item(0).InnerText = "100";
                xmlProperties.Save(corePart.GetStream());
    

    You can also refer to similar case below:

    http://social.msdn.microsoft.com/Forums/en-US/024fb9fa-cc0e-479a-84b3-d651a21eb988/how-to-programatically-access-builtin-properties-of-open-xml-word-doc?forum=oxmlsdk

    Regards,

    Marvin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 2, 2014 5:33 AM