Hello,
I have just started using Open XML and I am trying to do something really simple to get and set the core properties of a document. I am using the following code
public static void getProps(String fileName) { WordprocessingDocument document = null; try { document = WordprocessingDocument.Open(fileName, false); var docProps = document.PackageProperties; Console.WriteLine("Category " + docProps.Category); Console.WriteLine("ContentStatus " + docProps.ContentStatus); Console.WriteLine("ContentType " + docProps.ContentType); Console.WriteLine("Created " + docProps.Created); Console.WriteLine("Creator " + docProps.Creator); Console.WriteLine("Description " + docProps.Description); Console.WriteLine("Identifier " + docProps.Identifier); Console.WriteLine("Keywords " + docProps.Keywords); Console.WriteLine("Language " + docProps.Language); Console.WriteLine("LastModifiedBy " + docProps.LastModifiedBy); Console.WriteLine("LastPrinted " + docProps.LastPrinted); Console.WriteLine("Modified " + docProps.Modified); Console.WriteLine("Revision " + docProps.Revision); Console.WriteLine("Subject " + docProps.Subject); Console.WriteLine("Title " + docProps.Title); Console.WriteLine("Version " + docProps.Version); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { document.Close(); } }
and
public static void setProps(String fileName) { WordprocessingDocument document = null; try { document = WordprocessingDocument.Open(fileName, true); var docProps = document.PackageProperties; docProps.Category = "test21"; docProps.ContentStatus = "test21"; docProps.ContentType = "test21"; docProps.Created = DateTime.Now; docProps.Creator = "test21"; docProps.Description = "test21"; docProps.Identifier = "test21"; docProps.Keywords = "test21"; docProps.Language = "test21"; docProps.LastModifiedBy = "test21"; docProps.LastPrinted = DateTime.Now; docProps.Modified = DateTime.Now; docProps.Revision = "test21"; docProps.Subject = "test21"; docProps.Title = "test21"; docProps.Version = "test21"; } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { document.Close(); } }
after I call the setProps if I try to open the word document with word I will get an error that says
file xxx.docx cannot be opened because there is a problem with the contents
and then tells me that it has found some unreadable context and if I would like restore the document.
could someone tell me what I am doing wrong? and why this is happening.
cheers,
Ehsan