是否可以存储与 Word 文档中的特定表或单元格相关的隐藏元数据信息?
我正在尝试将元数据(基本上是唯一的 ID)与 Word 文档中表格的每个单元格一起存储。目前,对于我正在开发的加载项,我正在查询数据库,并使用检索到的数据在 Word 文档中构建一个表。
我希望能够保存用户对文档的任何编辑,并将其保留回数据库。我最初的想法是在表中的每个单元格中存储一个唯一的 ID,以便我能够知道要更新哪些记录。我还想在每个单元格中存储某种“isChanged”标志,以便我可以知道哪些单元格已更改。我发现我可以将所需的信息添加到单元格的“ID”属性中 - 但是,如果用户保存文档、关闭它并重新打开它,则不会保留该信息。然后,我尝试通过将数据添加到“Fields”集合来存储数据 - 但这不起作用并引发运行时错误。这是我尝试过的代码:
object t1 = Word.WdFieldType.wdFieldEmpty;
object val = "myValue: " + counter;
object preserveFormatting = true;
tbl.Cell(i, j).Range.Fields.Add(tbl.Cell(i, j).Range, ref t1, ref val, ref preserveFormatting);
这可以正常编译,但会抛出此运行时错误“此命令不可用”。
那么,这有可能吗?还是我走错了方向?
提前致谢。
I am trying to store metadata (basically a unique id) along with each cell of a table in a Word document. Currently, for the add-in I'm developing, I am querying the database, and building a table inside the Word document using the data that is retrieved.
I want to be able to save any of the user's edits to the document, and persist it back to the database. My initial thought was to store a unique id along with each cell in the table so that I would be able to tell which records to update. I would also like to store some sort of "isChanged" flag within each cell so that I could tell which cells were changed. I found that I could add the needed information into the "ID" property of the cell - however, that information was not retained if the user saved the document, closed it, and re-opened it. I then tried storing the data by adding a data to the "Fields" collection - but that did not work and threw a runtime error. Here is the code that I tried:
object t1 = Word.WdFieldType.wdFieldEmpty;
object val = "myValue: " + counter;
object preserveFormatting = true;
tbl.Cell(i, j).Range.Fields.Add(tbl.Cell(i, j).Range, ref t1, ref val, ref preserveFormatting);
This compiles fine, but throws this runtime error "This command is not available".
So, is this possible at all? Or am I headed in the wrong direction?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终使用“ContentControls”来存储我需要的信息。我使用“标题”字段来存储唯一 ID,使用“标签”字段来跟踪该字段是否已更改。有关详细信息,请参阅此链接:http://blogs.technet.com/gray_knowlton/archive/2010/01/15/associating-data-with-content-controls.aspx
Wound up using "ContentControls" to store the information I needed. I used the "Title" field to store the unique id, and the "tag" field to track whether the field had been changed or not. See this link for more info: http://blogs.technet.com/gray_knowlton/archive/2010/01/15/associating-data-with-content-controls.aspx
由于“Word 2007 文档”是 XML,因此您可以向文档添加命名空间,然后使用命名空间中的属性来使用元素。 Word 在加载和保存时应忽略您的命名空间。此外,您可以添加新元素来存储所需的任何信息(元数据)。
话虽如此,我还没有在 Word 中使用过这种技术,但我已经使用 Excel 2003 成功地完成了该技术。
首先要尝试的是创建一个裸露的“Word 2007 文档”。根据您的情况,添加一个简单的二乘二表。使用文本或 XML 编辑器打开它并添加您的名称空间,然后添加一个属性并添加一个元素。用Word打开进行更改然后保存。使用编辑器打开并确保您的名称空间属性和元素尚未更改。
Since a "Word 2007 Document" is XML, you can add a namespace to the document then adore the elements with attributes from your namespace. Word should ignore your namespace when loading and saving. Moreover, you can add new elments to store any information (metadata) needed.
With that said, I have not used this technique with Word, but I have done it successfully using Excel 2003.
First thing to try, is create a bare "Word 2007 Document". In your case, add a simple two by two table. Open it with a text or XML editor and add your namespace, and adore an attribute and add an element. Open with Word make a change then save it. Open with editor and make sure your namespace attribute and element have not been changed.