在 C# 2.0 中保存 Word 2007 文档属性
我正在尝试修改 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
我两分钟前解决了同样的问题。
当您添加/更改自定义属性时,文档似乎没有
更改
,因此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 theWordApplication.ActiveDocument.Saved
is stilltrue
.Set it to
false
and then call the Save method of the Document, it will work!!