首页 > 解决方案 > 由于某种原因,C# Word Interop 无法更改文档属性中内置的“创建日期”

问题描述

我使用的代码:

string fullPath = null;
DateTime creationDate = new DateTime();
string creatorName = null;
foreach (var arg in args) {
    if (arg.Contains("--path")) {
        fullPath = arg.Substring(7);
    }
    if (arg.Contains("--creationDate")) {
        creationDate = DateTime.Parse(arg.Substring(15));
    }
    if (arg.Contains("--creatorName")) {
        creatorName = arg.Substring(14);
    }
}

var wordInterop = new Microsoft.Office.Interop.Word.Application();
var wordWorkBook = wordInterop.Documents.Open(fullPath);

wordWorkBook.BuiltInDocumentProperties["Creation Date"].Value = creationDate;
wordWorkBook.BuiltInDocumentProperties["Author"].Value = creatorName;
wordWorkBook.Save();
wordInterop.Quit();
Marshal.ReleaseComObject(wordWorkBook);
Marshal.ReleaseComObject(wordInterop);

File.SetCreationTime(fullPath, creationDate);

每当我打印出创建日期时,它都保持不变。设置作者属性有效。

我尝试用这种方法欺骗 Excel,它有效。

标签: c#interopword

解决方案


推荐阅读