在 C# 2.0 中保存 Word 2007 文档属性

发布于 2024-08-06 19:20:13 字数 958 浏览 0 评论 0原文

我正在尝试修改 .docx 文档的一些 CustomDocumentProperties。我已经能够读取当前值并对其进行修改,但是当我保存文档时,对自定义字段的更改将会丢失。

我在 DocAccessor 类(用作我的文档文件的接口)中有以下方法:

void SetInfo(string key, string val) {
    object custom_properties = current_doc.CustomDocumentProperties;
    Type custom_properties_type = custom_properties.GetType();
    custom_properties_type.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, custom_properties, new object[] { key, val });
}

在其他地方我调用:

doc_accessor.GetInfo("Number") //returns 5
doc_accessor.SetInfo("Number", "6");
doc_accessor.GetInfo("Number") //returns 6
doc_accessor.SaveAndClose();
doc_accessor.Open(); //it retains the path, so I don't need to respecify
doc_accessor.GetInfo("Number") //returns 5

当我修改路径以保存到不同位置时,我的 doc_accessor.SaveAndClose() 函数工作正常,并且确实如此。 ..但没有编写修改后的 CustomDocumentProperties。这使得我看起来好像缺少某种提交步骤,但 current_doc.Save() 不应该处理这个问题吗?

I'm trying to modify some CustomDocumentProperties for a .docx document. I've been able to read the current value and modify it, but when I save the document the changes to the custom fields are lost.

I have the following method within a DocAccessor class (which serves as an interface for my doc files):

void SetInfo(string key, string val) {
    object custom_properties = current_doc.CustomDocumentProperties;
    Type custom_properties_type = custom_properties.GetType();
    custom_properties_type.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, custom_properties, new object[] { key, val });
}

elsewhere I call:

doc_accessor.GetInfo("Number") //returns 5
doc_accessor.SetInfo("Number", "6");
doc_accessor.GetInfo("Number") //returns 6
doc_accessor.SaveAndClose();
doc_accessor.Open(); //it retains the path, so I don't need to respecify
doc_accessor.GetInfo("Number") //returns 5

My doc_accessor.SaveAndClose() function is working correctly as I modified the path to save to a different location and it did... but without writing the modified CustomDocumentProperties. This makes it seem as if there's a commit step of sorts that I'm missing, but shouldn't current_doc.Save() handle that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花浅忆 2024-08-13 19:20:13

http://support.microsoft.com/kb/195425

http://msdn.microsoft.com/en-us/library/y1xatbkd(VS. 80).aspx

我不知道这些是否有帮助。
但这就是我要开始的地方。

http:// /msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documentclass.saved(office.11​​).aspx

抱歉,我不得不删除协议标题,因为堆栈我认为我的答案中不应该包含多个链接,因为我不是真正的会员

http://support.microsoft.com/kb/195425

http://msdn.microsoft.com/en-us/library/y1xatbkd(VS.80).aspx

i dunno if either of these will help.
but that's where i would start.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documentclass.saved(office.11).aspx

Sorry about the links i've had to remove the protocol heading because stack doesn't think i should be able to have more than one link in my answers because i'm not a real member

辞取 2024-08-13 19:20:13

我两分钟前解决了同样的问题。

当您添加/更改自定义属性时,文档似乎没有更改,因此WordApplication.ActiveDocument.Saved 仍然是true

设置为false然后调用Document的Save方法,就可以了!!

I solved 2 minutes ago the same problem.

When you add/change custom properties seems that the document is no changed, so the WordApplication.ActiveDocument.Saved is still true.

Set it to false and then call the Save method of the Document, it will work!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文