自定义文档 属性 未保存在 Word 文档中
Custom Document property not getting saved in Word Document
我已经为 word 创建了一个插件。我正在尝试通过单击按钮更新 word 文档中自定义 属性 的值。但它没有得到保存。
我写的代码是:
private void button_Click(object sender, IRibbonControl control, bool pressed)
{
Word.Document document = WordApp.ActiveDocument;
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)document.CustomDocumentProperties;
properties["abc"].Value = "newValue";
document.Save();
}
如果我关闭文档并再次打开它,我得到的是旧值而不是新值。
但是如果我在我的文档中添加一个space然后保存它。然后自定义 属性 的值被保存。
代码是:
private void button_Click(object sender, IRibbonControl control, bool pressed)
{
Word.Document document = WordApp.ActiveDocument;
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)document.CustomDocumentProperties;
properties["abc"].Value = "newValue";
document.Range(document.Content.End - 1, document.Content.End - 1).Select();
WordApp.Selection.Range.Text = " ";
document.Save();
}
为什么行为是这样的。我不想在我的文档中添加任何额外的空白 space。请帮助我。提前致谢。
这是许多 Office 应用程序中已知的 "idiosyncracy",而不仅仅是 Word。更改文档的值 属性,但没有别的,不会得到 "noticed",因此不会保存。这个 discussion on MSDN.
中有相当多的细节
要么代码需要向文档 "body" 添加一些内容(然后可以将其删除但 不会 撤消)或者它可以显式设置 "dirty" 标记文档,以便 Word 意识到它确实需要保存:
document.Saved = false;
document.Save();
我已经为 word 创建了一个插件。我正在尝试通过单击按钮更新 word 文档中自定义 属性 的值。但它没有得到保存。 我写的代码是:
private void button_Click(object sender, IRibbonControl control, bool pressed)
{
Word.Document document = WordApp.ActiveDocument;
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)document.CustomDocumentProperties;
properties["abc"].Value = "newValue";
document.Save();
}
如果我关闭文档并再次打开它,我得到的是旧值而不是新值。
但是如果我在我的文档中添加一个space然后保存它。然后自定义 属性 的值被保存。 代码是:
private void button_Click(object sender, IRibbonControl control, bool pressed)
{
Word.Document document = WordApp.ActiveDocument;
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)document.CustomDocumentProperties;
properties["abc"].Value = "newValue";
document.Range(document.Content.End - 1, document.Content.End - 1).Select();
WordApp.Selection.Range.Text = " ";
document.Save();
}
为什么行为是这样的。我不想在我的文档中添加任何额外的空白 space。请帮助我。提前致谢。
这是许多 Office 应用程序中已知的 "idiosyncracy",而不仅仅是 Word。更改文档的值 属性,但没有别的,不会得到 "noticed",因此不会保存。这个 discussion on MSDN.
中有相当多的细节要么代码需要向文档 "body" 添加一些内容(然后可以将其删除但 不会 撤消)或者它可以显式设置 "dirty" 标记文档,以便 Word 意识到它确实需要保存:
document.Saved = false;
document.Save();