简体   繁体   English

VBA Excel图片到Word书签宏

[英]VBA Excel Picture to Word Bookmark macro

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. 我正在尝试修改VBA宏,以将Excel范围(作为图片用于格式化)粘贴到Word书签。

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

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. 宏会粘贴单个单元格文本引用规则(“ A34”),但是对范围“ BTM_PREM”使用相同的代码会返回类型不匹配错误。 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. 我知道这是由于范围不是字符串引起的,但是似乎无法识别如何修改此行以启用“ BTM_PREM”作为图片粘贴在“ PLAN_2_SHEET”书签中。

.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value

you could use Copy() method on Excel Range object and then either Paste() or PasteSpecial() or PasteExcelTable() Word Range object methods, like follows: 您可以在Excel Range对象上使用Copy()方法,然后在Paste()PasteSpecial()PasteExcelTable() Word Range对象方法上使用,如下所示:

ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.Paste

or 要么

ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteSpecial Link:=True

or 要么

ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteExcelTable LinkedToExcel:=True, WordFormatting:=False, RTF:=True

This is a piece of code that works for me: 这是一段对我有用的代码:

ActiveWorkbook.Sheets("Lease 1").Range("B16:AF25").CopyPicture Appearance:=xlScreen, Format:=xlPicture
wdoc.Bookmarks("Bkmrk1").Range.Paste
Application.CutCopyMode = False 

It's not a complete macro, just a part of it so you'll have to adjust a bit, But I think you get the idea. 它不是一个完整的宏,只是其中的一部分,因此您必须进行一些调整,但是我想您已经明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM