[英]VBA to copy and process paragraphs and tables from one Word document to another
My scenario is that I want to copy and clean up SOME of the content from Word doc1 and store the sanitized content (with newly assigned styles) in doc2.我的情况是,我想从 Word doc1 复制和清理一些内容,并将清理过的内容(具有新分配的样式)存储在 doc2 中。 I know how to loop over the paragraphs, recognize the ones that I want, and copy them into doc2 and how to assign new styles there.
我知道如何循环段落,识别我想要的段落,并将它们复制到 doc2 以及如何在那里分配新样式。
I can also loop over the doc1.Tables collection and copy tables.我还可以遍历 doc1.Tables 集合并复制表。
But I cannot figure out how to place the doc1 tables in the appropriate place in doc2.但我无法弄清楚如何将 doc1 表放在 doc2 中的适当位置。 I would love to use a loop like this that kept the relative positions of paragraphs and tables consistent:
我很想使用这样的循环,使段落和表格的相对位置保持一致:
Foreach (object o in doc1)
if (o.type = paragraph) then
if (I like the paragraph) then
copy the paragraph to doc2
endif
else if (o.type = table) then
if (I like the table for copying) then
copy the table to doc2
end if
endif
next
But I don't know if that loop style (looping over objects) is possible or reasonable in VBA.但我不知道这种循环样式(循环对象)在 VBA 中是否可行或合理。 I can't find any examples of it.
我找不到任何例子。 Any other approach needs some way to associate a table with some position, bookmark, range, or text in the sequence of paragraphs in the document.
任何其他方法都需要某种方式将表格与文档中段落序列中的某个位置、书签、范围或文本相关联。 But since I am currently only copying part of the doc1 text, I could easily not copy over whatever the tables are anchored to.
但由于我目前只复制 doc1 文本的一部分,因此我无法轻松复制表所定位的任何内容。
I'm almost to the point of copying over the "wholestory" of doc1 to doc2 and then deleting parts that I don't need and restyling parts that I do need, all in situ.我几乎要复制 doc1 的“整个故事”到 doc2,然后删除我不需要的部分并重新设计我需要的部分,所有这些都在原地进行。
Is it possible to use the kind of loop shown above to sequentially copy things (paragraphs, tables, images, etc.) from doc1 to doc2?是否可以使用上面显示的那种循环将内容(段落、表格、图像等)从 doc1 依次复制到 doc2? If so, what does the object testing code look like?
如果是这样,对象测试代码是什么样的? (
if o.type is type.paragraph
)? (
if o.type is type.paragraph
)? Should I even be trying that approach, or is it a waste of time?我应该尝试这种方法,还是浪费时间? Thank you
谢谢
If you're looping paragraphs, check whether the paragraph is in a table.如果您要循环段落,请检查该段落是否在表格中。 That will let you do things in order, as far as tables are concerned
就表格而言,这将使您按顺序做事
Foreach (object o in doc1)
if (o.type = paragraph) then
if (paragraph.Range.Information(wdWithinTable) Then
do something with the entire table
elseif (I like the paragraph) then
copy the paragraph to doc2
endif
next
Note, however, that there's really no reliable way to handle all possible Word objects using the object model.但是请注意,使用对象模型处理所有可能的Word 对象确实没有可靠的方法。 Most especially, graphical objects formatted with text wrap (as opposed to in-line) are very complex.
最特别的是,用文本换行(而不是内联)格式化的图形对象非常复杂。
The more reliable way to work through document content in the order of "things" is to leverage the Word Open XML file format, where the actual underlying content is available.按照“事物”的顺序处理文档内容的更可靠方法是利用 Word Open XML 文件格式,其中实际的基础内容可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.