简体   繁体   English

VBA 将段落和表格从一个 Word 文档复制和处理到另一个

[英]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.

相关问题 使用 Word 宏/VBA 将表格从一个 Word 文档复制到另一个 Word 文档 - Use Word Macro/VBA to Copy Tables from One Word Document to Another Word Document 从Xcel Vba中从一个Word文档中选择一系列文本,然后复制到另一个Word文档中 - from Xcel Vba select a range of text from one Word document and copy into another Word document 使用vba将ComboBox从Word文档复制到另一个Word文档 - Copy ComboBox from a Word Document to another Word Document with vba select 使用 vba 并复制到另一个文档的末尾并保留格式的一个 Word 文档中的一系列文本 - select a range of text from one Word doc using vba and copy to end of another document and RETAIN formatting 一个接一个地复制/粘贴两个Word文档中的后续段落(学习外语) - Copy/paste subsequent paragraphs from two Word documents one after another (to learn a foreign language) 使用vba将word文档的内容复制到另一个word文档中 - Using vba to copy the contents of a word document into another word document 将图表 excel 复制到带有 VBA 的特定段落单词 - copy Charts excel to specific paragraphs word with VBA 使用vba从xls文档复制到word - copy from xls document into word using vba 从一个 Word 文档中选择一系列文本并复制到另一个 Word 文档中 - select a range of text from one Word document and copy into another Word document Select 来自一个 Word 文档的部分文本并复制到另一个 Word 文档中 - Select some parts of text from one Word document and copy into another Word document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM