简体   繁体   English

VBA - 从 Excel * 不* 使用剪贴板导出图像(复制/粘贴)

[英]VBA - Export Image from Excel *without* using Clipboard (Copy/Paste)

There are a lot of great examples of how to take an Excel range, create an image from it, and save it to the drive.有很多很好的示例说明如何获取 Excel 范围、从中创建图像并将其保存到驱动器。 Here is one: Export pictures from excel file into jpg using VBA这里是一: 使用VBA将excel文件中的图片导出为jpg

This works great on a small scale, but when you try to run this through 3,000 or more iterations, a "memory leak" caused by the repeated use of the clipboard eventually erodes the process and the macro fails somewhere along the way.这在小规模上效果很好,但是当您尝试通过 3,000 次或更多次迭代运行它时,由重复使用剪贴板引起的“内存泄漏”最终会侵蚀该过程,并且宏在此过程中的某个地方失败。 This occurs even when running 64-bit Excel on a powerful machine (50+ GB of RAM).即使在功能强大的机器(50+ GB 的 RAM)上运行 64 位 Excel 也会发生这种情况。

Are there any ways to do this without using the clipboard??有没有办法在不使用剪贴板的情况下做到这一点? My first thought was to try to fix the memory leak issue, but all of those attempts have been unsuccessful.我的第一个想法是尝试修复内存泄漏问题,但所有这些尝试都没有成功。 For context, I'm basically using the exact code as provided in the solution on link above (with a couple of added features to try to reduce memory leaking like auto-saving the workbook after every 100 images, etc.).对于上下文,我基本上使用上面链接的解决方案中提供的确切代码(添加了一些附加功能以尝试减少内存泄漏,例如每 100 张图像后自动保存工作簿等)。

I'm also looking for what you mentioned;我也在找你提到的; here's how to do it with a chart:以下是使用图表的方法:

Dim file As String ' the path to the saved image, in the temp dir
file = Environ$("temp") & "\chart.gif"

Sheets("Sheet1").ChartObjects(0).Activate
Sheets("Sheet1").ChartObjects(0).Chart.Export Filename:=file, FilterName:="GIF"

There was ultimately no solution for the memory leak, it seems to be a systemic problem with VBA.内存泄漏最终没有解决方案,这似乎是 VBA 的系统问题。

For those trying to programmatically generate charts, it is much easier to build in PHP.对于那些试图以编程方式生成图表的人来说,用 PHP 构建要容易得多。

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

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