VBA Word复制表
提前抱歉我的英语不好,可能是一个简单的问题。 我想将 Table 对象复制到 Dictionary 中,以便稍后将其放入另一个 Word 文档中。所有作品均采用 Excel VBA 编写。 我尝试将其用于复制表:
Dim dict As Dictionary
Dim table1 As Word.table
Set table1 = Word.Application.ActiveDocument.Tables.Item(tableNum)
dict.Add "SampleText","MyText"
dict.Add "tab1", table1
之后我关闭此文档并打开另一个文档。在其中,我尝试将数据从字典插入到书签:
dim prilDoc As Word.Document
...
prilDoc.Bookmarks.Item("SampleText").Range.Text=dict.Item("SampleText")
这个(插入文本)工作正常,但我不知道如何将表对象放在书签位置,因为我不知道VBA Word对象模型。我尝试了一些方法,但找不到解决方案。我认为有两个问题:
当我将 Table 对象放入 Dictionary 时,实际上,我仅复制了第一个 Word 文档中对 Table 对象的引用。当我关闭此文档并尝试从 Dictionary 对象中获取它时,它被删除,我无法使用它。在Java中我可以深度克隆对象,但我不知道如何在VBA中做到这一点。可能这里必须完全使用另一种方法。
我不知道,我必须如何正确地在新的Word文档中插入Table对象。
提前致谢。
Sorry in advance for my bad English and may be simple question.
I want to copy Table object into Dictionary for place it later in another Word document. All works are in Excel VBA.
I try it for copy table:
Dim dict As Dictionary
Dim table1 As Word.table
Set table1 = Word.Application.ActiveDocument.Tables.Item(tableNum)
dict.Add "SampleText","MyText"
dict.Add "tab1", table1
After it I close this document and open another. In it, I try to insert data from Dictionary to Bookmarks:
dim prilDoc As Word.Document
...
prilDoc.Bookmarks.Item("SampleText").Range.Text=dict.Item("SampleText")
this (insert text) work fine, but I don't know, how to place Table object on Bookmark place, because I don't know VBA Word Object Model. I tryed some approaches, but can't find solution. I think there are two problems:
When I put Table object into Dictionary, I in fact, copyed only reference to Table object in first Word document. And when I close this document and I try to get it from Dictionary object is deleted and I can't work with it. In Java I can deep cloning object, but I don't know how to do it in VBA. May be here must be use fully another approach.
I don't know, how correctly must I insert Table object in new Word document.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试在打开第一个文档的情况下进行复制?在复制内容之前不要关闭它。
Did you try copying with the first document open? Don't close it before the contents are copied over.