VBA-范围为jpg图片 [英] VBA - Range to jpg picture
问题描述
我正在尝试从excel中的特定范围获取jpg文件
I'm trying to get a jpg file from a specific range in excel
我目前正在
_Worksheet对象的Range方法发生1004运行时错误.
1004 Runtime error on Range method from _Worksheet object.
这是我的代码的样子:
Sub Export()
Dim ws As Worksheet
Dim Rng As Range
Dim Chrt As Chart
Set ws = ActiveSheet
Set Rng = Range("B2:H11")
ws.Range(Rng).CopyPicture
Set Chrt = Charts.Add
With Chrt
.Paste
.Export FileName = "Case.jpg", Filtername:="JPG"
End With
End Sub
推荐答案
在他的评论中已经提到了主要错误@J_Lard.
The main error has @J_Lard mentioned already in his comment.
但是我会使用ChartObject
而不是Chart
表.这样您就可以确定输出的大小,而不是获得图片中的整个图表区域.
But I would use ChartObject
rather than a Chart
sheet. Whith this you can determine the size of the output instead of getting the whole chart area in the picture.
在使用F8
步骤时,粘贴和导出将起作用,而在实时运行时,需要激活ChartObject
.
And while using F8
step the paste and export will work, while real time run, the ChartObject
needs to be activated.
Sub Export()
Dim oWs As Worksheet
Dim oRng As Range
Dim oChrtO As ChartObject
Dim lWidth As Long, lHeight As Long
Set oWs = ActiveSheet
Set oRng = oWs.Range("B2:H11")
oRng.CopyPicture xlScreen, xlPicture
lWidth = oRng.Width
lHeight = oRng.Height
Set oChrtO = oWs.ChartObjects.Add(Left:=0, Top:=0, Width:=lWidth, Height:=lHeight)
oChrtO.Activate
With oChrtO.Chart
.Paste
.Export Filename:="Case.jpg", Filtername:="JPG"
End With
oChrtO.Delete
End Sub
如果未指定路径,则默认情况下将保存Case.jpg
保存位置.这可能是您的用户文档目录C:\Users\YourName\Documents\
If path is not specified, the Case.jpg
will be saved in default
save location. This is probably your user documents directory C:\Users\YourName\Documents\
这篇关于VBA-范围为jpg图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!