使用 VBA 将表格从 Excel 粘贴到 Word 作为“使用目标样式”粘贴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18639840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Paste table from Excel to Word as "Use Destination Styles" paste using VBA
提问by Ryan
I have a macro that currently copies a table from a range of cells in an Excel spreadsheet, then pastes it to a Word document (which was created earlier in the macro as WordApp). However, I'm hoping to have it paste into the Word doc with the paste option "Use Destination Styles" that is one of the paste style options in Word. Does anyone know if there is a way to embed that into the Excel's VBA code?
我有一个宏,它当前从 Excel 电子表格中的一系列单元格中复制一个表格,然后将其粘贴到一个 Word 文档(该文档之前在宏中创建为 WordApp)。但是,我希望使用粘贴选项“使用目标样式”将其粘贴到 Word 文档中,该选项是 Word 中的粘贴样式选项之一。有谁知道是否有办法将其嵌入到 Excel 的 VBA 代码中?
Here is my current code (assume that "WordApp" is dim word application and a new doc has already been accounted for in the code).
这是我当前的代码(假设“WordApp”是暗字应用程序,并且代码中已经包含了一个新文档)。
'Select table data
Sheets("Sheet3").Select
Range("I11:P18").Select
Selection.Copy
'Paste table into word doc
WordApp.Selection.TypeParagraph
WordApp.Selection.Paste
Any help would be greatly appreciated so I can finally finish this gigantic VBA project!
任何帮助将不胜感激,所以我终于可以完成这个巨大的 VBA 项目!
EDIT:
编辑:
Unfortunately, that doesn't work--it pastes it in the format I don't want. I tried using this code, but i'm getting a run time error 5342, which says "The specified data type is unavailable" and highlights .PasteAndFormat line of code. Does anyone know why there is an error? I got that code from the object browser, so I'm not sure why it doesn't recognize it:
不幸的是,这不起作用——它以我不想要的格式粘贴它。我尝试使用此代码,但出现运行时错误 5342,其中显示“指定的数据类型不可用”并突出显示 .PasteAndFormat 代码行。有谁知道为什么会出现错误?我从对象浏览器中获得了该代码,所以我不确定为什么它不能识别它:
Sheets("Sheet3").Select
Range("H21:P34").Select
Selection.Copy
'Paste Q1-7 table into word doc
WordApp.Selection.PasteAndFormat (WdPasteOptions.wdUseDestinationStyles)
Please, anyone who can solve this, I would really appreciate it. This is a final detail on a gigantic macros project that I've been working on, and I just want to wrap this up.
拜托,任何人都可以解决这个问题,我将不胜感激。这是我一直在研究的一个巨大宏项目的最后一个细节,我只想结束它。
回答by Ryan
I figured it out on my own. Here's the code that pastes it as a table, the way that I wanted it to be pasted, without having to use word's paste as destination styles option.
我自己想出来的。这是将其粘贴为表格的代码,我希望粘贴它的方式,而不必使用 word 的粘贴作为目标样式选项。
Sheets("Sheet3").Select
Range("H21:P34").Select
Selection.Copy
'Paste Q1-7 table into word doc
WordApp.Selection.PasteExcelTable False, True, False
回答by Joe Laviano
WordApp.Selection.Range.PasteSpecial DataType:=wdPasteText
Does this work? I have this for a macro that pastes just one cell text into Word, but I don't have resources available to test this in Word. (I would've just written a comment if I had 50 rep, so sorry for making a whole answer for this).
这行得通吗?我有一个只将一个单元格文本粘贴到 Word 中的宏,但我没有可用的资源来在 Word 中测试它。(如果我有 50 个代表,我会写评论,很抱歉为此做出完整的答案)。