VBA Excel图片到Word书签宏 [英] VBA Excel Picture to Word Bookmark macro
问题描述
我正在尝试修改VBA宏,以允许将Excel范围(作为图片用于格式化)粘贴到Word书签.
I am trying to amend a VBA macro to enable pasting of an Excel range (as a picture, for formatting purposes) to a Word bookmark.
Sub test2()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("PREMIUMS")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\TEST\BTM Macro Template.docx"
With objWord.ActiveDocument
.Bookmarks("PLAN_1_SHEET").Range.Text = ws.Range("A34").Value
.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value
End With
Set objWord = Nothing
End Sub
宏将粘贴单个单元格文本引用规则("A34"),但是对范围"BTM_PREM"使用相同的代码将返回类型不匹配错误. 我知道这是因为范围不是字符串,但是似乎无法识别如何修改此行以启用"BTM_PREM"作为图片粘贴在"PLAN_2_SHEET"书签中.
The macro pastes a single cell text reference fine ("A34"), but using the same code for a range "BTM_PREM") returns a type mismatch error. I know it is due to the range not being a string, but can't seem to identify how to amend this line to enable pasting of "BTM_PREM", as a picture, at the "PLAN_2_SHEET" bookmark.
.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value
推荐答案
您可以在Excel Range
对象上使用Copy()
方法,然后使用 PasteSpecial()
或 Range
对象方法,如下所示:
you could use Copy()
method on Excel Range
object and then either Paste()
or PasteSpecial()
or PasteExcelTable()
Word Range
object methods, like follows:
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.Paste
或
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteSpecial Link:=True
或
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteExcelTable LinkedToExcel:=True, WordFormatting:=False, RTF:=True
这篇关于VBA Excel图片到Word书签宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!