使用 VBA 插入一张图片,尺寸在 Excel 2010 和 Excel 2016 之间不同

Insert a picture using VBA, different size between Excel 2010 and Excel 2016

我写这篇文章是因为我正在努力理解为什么在使用 VBA.

导入图片时会得到 2 个不同的结果

图片来源为: https://maps.googleapis.com/maps/api/staticmap?size=390x280&zoom=8&maptype=terrain&markers=color:red%7Clabel:A%7C18.510516,-72.287768&key=mykey

我正在使用此 VBA 代码在单元格 Z8 中插入图片:

Range("Z8").Select
ActiveSheet.Pictures.Insert(URL).Select
With Selection
.ShapeRange.ScaleWidth 0.999, msoTrue
End With

*在这种特定情况下不需要 .ShapeRange.ScaleWidth 0.999 但即使我删除 With Selection 指令,我也会得到相同的输出。

如果我 运行 Excel 2010 年的 VBA 代码,用于构建工作表,一切看起来都很棒,如下所示:

但是当我 运行 Excel 2016 年的 VBA 代码时,我得到了一些不同的东西:

本例中的图片较大,超出了页面限制。您可以在左上角看到的二维码也发生了同样的情况。垂直(和水平)分页符也移动了(我无法解释原因)但我会把这个问题留到另一条消息中。

一般来说 Excel 2016 的图片比 Excel 2010 的图片大 1.25。 Excel 2016 安装在另一台 不同屏幕的笔记本电脑上分辨率。这可能是我面临的问题的原因吗?如果是这样,您是否知道一种以与屏幕分辨率无关的方式修复它的方法?

谢谢,斯特凡诺

Microsoft 从 Excel 2010 年开始引入了新的图片分辨率和压缩处理流程,然后随着技术在未来几年的快速改进而调整了默认设置。这可能与您的问题有关。

您的计算机之间的不同监视器 sizes/resolution 也会影响默认(和可用)设置。

Even though the monitors are different sizes, it would be helpful to try to set your Windows Display Resolution to the same value on both, at least temporarily.

The steps vary slightly depending on which version of Windows you're running, but they're not hard to Google. (For example, here are instructions for Windows 7 installations.)

检查 两台 计算机上的以下设置以查看哪些选项可用(不是greyed-out) 更改任何内容之前,因为您需要决定两者都可用的设置。


默认分辨率设置

  • 在Excel中点击FileOptions
  • 在对话框的左侧,选择Advanced
  • 在对话框的右侧,向下滚动到 Image Size & Quality
  • 注意哪些设置是selected,哪些是可用:

    ☑ Do not compress images in file(复选框)

    Default Resolution (drop-down)

对于下一步,您需要在工作表上有图片,因此您也可以使用示例中的 "problem images"。

  • Single-click图片到select吧。这应该会显示 Picture Tools / Format 菜单。

  • 点击Compress Pictures

  • 在打开的对话框中,在 Resolution 下,记下哪些设置是 selected,哪些是 可用。

希望两台机器上都有可用的组合。将它们设置为相同后,重新启动两台机器,然后再次尝试插入图片。

正如我所提到的,影响它的因素有很多。确保图形在两台不同的机器上表现相同的唯一方法是它们具有相同的软件 图形硬件。


如果所有其他方法都失败了并且您想再试一次 long-shot,注册表设置 可能会影响一台或两台机器。

很难找到有关 Office 数以千计的注册表设置的文档(许多默认情况下不使用,例如这个)- 其中一个 source 暗示这仅影响转换为网页的 Office 文档中图像的分辨率。

但是,我相信如果您 曾经 将 Excel 文档保存为网页,则此注册表项(和它相关 "known issue" ) 也可能受到影响,因此您可以 double-check.


注册表"Hack"

要解决此问题,请添加以下注册表项以防止图像大小发生变化。

Standard Registry Warning: Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall your operating system. Microsoft cannot guarantee that these problems can be solved. Modify the registry at your own risk.

  1. Exit all programs.

  2. Hit the Windows Key, type regedit, and then hit Enter.

  3. Locate and then select the following registry subkey:

    HKEY_CURRENT_USER\Software\Microsoft\Office\Common

  4. After you select the subkey, click the Edit menu, click New and choose DWORD Value.

  5. Type DownRevRasterizationDPI, and then press Enter.

  6. Right-click DownRevRasterizationDPI, and then click Modify.

  7. In the Value data box, type a value in the range of 1 to 300 and then click OK.

In the Value data box, set a value of the resolution that you want to specify for the image. For example, if you set 96, which is a default screen resolution value for Windows, and insert a screenshot into Excel, the image resolution of the screenshot becomes 96 dpi.

  1. File 菜单上,单击 Exit 退出注册表编辑器。

通常我喜欢在我的回答中包含 link 到 "More Information",但我担心 除了这些之外没有太多内容来源 link(上图)。

祝你好运!