vba 图片类的插入属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20415934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:50:16  来源:igfitidea点击:

Insert Property of Picture Class

excelimagevbaexcel-vba

提问by kentcdodds

I have read all of the questions on here about this topic and none of them provided me with a workable solution, so I'm asking this one.

我已经阅读了这里关于这个主题的所有问题,但没有一个为我提供了可行的解决方案,所以我问了这个问题。

I am running a legitimate copy of Excel 2013 in Windows 7. I record a macros where I insert a picture, and in the open file dialog I paste this URL: http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg(simply a picture of a product on Amazon). This works as expected.

我在 Windows 7 中运行 Excel 2013 的合法副本。我在插入图片的地方录制了一个宏,然后在打开的文件对话框中粘贴了这个 URL:(http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg只是亚马逊上的产品图片)。这按预期工作。

The resulting macros looks like this:

生成的宏如下所示:

Sub insertImage()
'
' percent Macro
'

'
    ActiveSheet.Pictures.Insert( _
        "http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg").Select
End Sub

However, when I attempt to run this, the Insertline breaks with the following error:

但是,当我尝试运行它时,Insert换行符出现以下错误:

Run-time error '1004':

Unable to get the Insert property of the Picture class

I am trying to insert a number of pictures into an excel document and I am using the ActiveSheet.Pictures.Insertmethod to do this. I have been experiencing this issue there, so I recreated it in a way others could replicate to facilitate getting an answer...

我正在尝试将一些图片插入到一个 excel 文档中,我正在使用该ActiveSheet.Pictures.Insert方法来执行此操作。我一直在那里遇到这个问题,所以我以其他人可以复制的方式重新创建它以促进获得答案......

An interesting thing to note is:

值得注意的一件有趣的事情是:

http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg 'This is what I pasted
http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg 'This is what the recorded macros recorded

It looks like Excel automatically resolved the %2Bto a +. I tried making that change, but to no success.

看起来 Excel 自动将 解析%2B+. 我尝试进行这种更改,但没有成功。

Another interesting thing to note is that sometimes this does work and sometimes it doesn't. This url is a case where it does not work. Here's one where it does: http://ecx.images-amazon.com/images/I/51mXQ-IjigL._SL160_.jpg

另一个需要注意的有趣的事情是,有时这确实有效,有时则无效。这个网址是一个不起作用的情况。这是它的一个地方:http://ecx.images-amazon.com/images/I/51mXQ-IjigL._SL160_.jpg

Why would Excel generate a macros it can't run? More importantly, how can I avoid this error and get on with my work!? Thanks!

为什么 Excel 会生成无法运行的宏?更重要的是,我怎样才能避免这个错误并继续我的工作!?谢谢!

采纳答案by Jerome Montino

Try this workaround:

试试这个解决方法:

Sub RetrieveImage()
Dim wsht As Worksheet: Set wsht = ThisWorkbook.ActiveSheet
wsht.Shapes.AddPicture "http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg", _
                    msoFalse, msoTrue, 0, 0, 100, 100
End Sub

All fields are required, which is kind of a bummer since you cannot get the default size. The location offsets and sizes are in pixels/points. Also, the %turning to +is just alright, as %would cause it to be not recognized (dunno why).

所有字段都是必需的,这有点令人失望,因为您无法获得默认大小。位置偏移和大小以像素/点为单位。此外,%转向+也没关系,因为%会导致它不被识别(不知道为什么)。

Result:

结果:

enter image description here

在此处输入图片说明

Let us know if this helps.

如果这有帮助,请告诉我们。

回答by RoelVB

I'm experiencing the same issue. After some digging I found out Excel does a HTTP HEAD request before getting the image. If this HEAD request is unsuccessful Excel will return the exact error messages mentioned in this discussion.

我遇到了同样的问题。经过一番挖掘,我发现 Excel 在获取图像之前会执行 HTTP HEAD 请求。如果此 HEAD 请求不成功,Excel 将返回本讨论中提到的确切错误消息。

Your could easily test this using Fiddler.

您可以使用Fiddler轻松测试。