|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
各位大佬,vba将数据内容存储为图片格式,代码分步运行的时候没有问题,整体运行可以保存图片,但是一张空白的图片,不知道什么原因,代码如下:
Sub 保存为JPG图片()
Dim dwk As Workbook, dst As Worksheet
Dim j2Value As String, b2Value As String, d2f2Value As String, fileName As String
Dim savePath As String
Dim rng As Range
Dim chartObj As ChartObject
' 获取保存路径和文件名
j2Value = ThisWorkbook.Sheets("筛选结果").Range("J2").Value
b2Value = ThisWorkbook.Sheets("筛选结果").Range("B2").Value
d2f2Value = Join(Application.Transpose(Application.Transpose(ThisWorkbook.Sheets("筛选结果").Range("D2:F2").Value)), "")
fileName = j2Value & b2Value & d2f2Value
savePath = "E:\导出\数据\" & fileName & ".jpg"
' 打开工作簿并设置工作表对象
Set dwk = Workbooks("志愿单.xlsm")
Set dst = dwk.Worksheets("Sheet1")
' 获取要保存为图片的区域
Set rng = dst.Range("a12").CurrentRegion
' 复制区域到剪贴板
rng.CopyPicture xlScreen, Format:=xlPicture
' 创建一个临时图像对象
Set chartObj = dst.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
chartObj.Chart.Paste
chartObj.Chart.Export fileName:=savePath, FilterName:="JPG"
' 删除临时图像对象
chartObj.Delete
MsgBox "保存为 JPG 成功!路径:" & savePath
End Sub
|
|