none
Copying Custom Document Properties from one Word Document to another RRS feed

  • Question

  • I am trying to copy all the custom document properties from one word document to another.  Are there any examples of this in c# - I found Ken Getz's article using VB -

     

    Thanks

    Monday, June 7, 2010 9:39 PM

Answers

  • I found my solution....

                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(strFileName, false))
                {
                    MainDocumentPart mainPart = wordDoc.MainDocumentPart;
                    var customProps = wordDoc.CustomFilePropertiesPart;
                    foreach (var p in customProps.Properties.Elements<op.Property>())
                    {
                        strName = p.Name;
                        myValue = p.InnerText;
                        try
                        {
                            if (strName != "_AssemblyLocation" && strName != "_AssemblyName" && strName != "Solution ID")
                            {
                                if (p.VTLPWSTR != null)
                                {
                                    cProps.Add(strName, false, Office.MsoDocProperties.msoPropertyTypeString, myValue, missing);
                                }
                                if (p.VTInt32 != null)
                                {
                                    cProps.Add(strName, false, Office.MsoDocProperties.msoPropertyTypeNumber, System.Convert.ToInt32(myValue), missing);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                    }
                }
            }

    Wednesday, June 23, 2010 6:27 PM

All replies

  • Hello Jocrews,

    Ken Getz has written a Visual How to articles in MSDN documentations,

    http://msdn.microsoft.com/en-us/library/bb308936(office.12).aspx

    There are codes in C# about how to set custom Word document properties. Is this what you are looking for?

     

    Best regards,

    Ji Zhou


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, June 8, 2010 9:21 AM
    Moderator
  • Not really - what I want to be able to do is to copy the package part that contains all of the custom document properties.


    Tuesday, June 8, 2010 2:58 PM
  • I found my solution....

                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(strFileName, false))
                {
                    MainDocumentPart mainPart = wordDoc.MainDocumentPart;
                    var customProps = wordDoc.CustomFilePropertiesPart;
                    foreach (var p in customProps.Properties.Elements<op.Property>())
                    {
                        strName = p.Name;
                        myValue = p.InnerText;
                        try
                        {
                            if (strName != "_AssemblyLocation" && strName != "_AssemblyName" && strName != "Solution ID")
                            {
                                if (p.VTLPWSTR != null)
                                {
                                    cProps.Add(strName, false, Office.MsoDocProperties.msoPropertyTypeString, myValue, missing);
                                }
                                if (p.VTInt32 != null)
                                {
                                    cProps.Add(strName, false, Office.MsoDocProperties.msoPropertyTypeNumber, System.Convert.ToInt32(myValue), missing);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                    }
                }
            }

    Wednesday, June 23, 2010 6:27 PM
  • DocumentProperties cProps;
                            cProps = wordDoc.CustomFilePropertiesPart as Office.DocumentProperties;
    cProps is always null or may be am wrong with this code , so please can anyone tell me
    Wednesday, May 25, 2011 1:43 PM