How to get Document Content Created Date using c#

夙愿已清 提交于 2020-01-25 00:29:05

问题


I'm creating a MS Word addin and would like to know how I can get the Content Created Date of the Active Document. This field can be found by going to the Properties of the Document, and then in the Details tab.

See my code below so far. However, this is returning the an incorrect date value of "1/01/1601 11:00:00 AM". The actual Content Created Date is "05/09/2015 11:53AM"

            string docName = Globals.ThisAddIn.Application.ActiveDocument.Name;

            string res = Path.GetFileNameWithoutExtension(docName);

            string fileloc = Path.GetFullPath(docName);

            FileInfo fi = new FileInfo(fileloc);
            Word.Application objApplication = Globals.ThisAddIn.Application;
            Word.Selection objSelection = objApplication.Selection;
            Word.Range objRange = objSelection.Range;
            objRange.InsertAfter(fi.CreationTime.ToString());
            objRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
            objRange.Select();

回答1:


No need to use the file. Just use the built-in document properties:

    internal DateTime GetContentCreatedDate()
    {
        Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
        Office.DocumentProperties properties = (Office.DocumentProperties)doc.BuiltInDocumentProperties;
        return (DateTime)properties[Word.WdBuiltInProperty.wdPropertyTimeCreated].Value;
    }


来源:https://stackoverflow.com/questions/32776737/how-to-get-document-content-created-date-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!