我有一个相当简单的sub,但它似乎不起作用:
Sub Test()
Dim p as object
strpath = "http://www.someurl.com/sample.jpg" ' some jpg
Set p = ActiveSheet.Shapes.AddPicture(strpath, False, False, 100, 100, 86, 129)
End Sub当我这样做时,它会给我运行时错误1004,指定值超出范围。但是,如果我将第二个"false“更改为"true",它将正常工作并插入图片。根据this的说法,第二个true/false影响是否将图片与文档一起保存。我不希望这种情况发生。
有人知道为什么会这样吗?
发布于 2013-12-01 00:47:03
这对我很有效:
Sub sof20301212ShapesAddpicture()
Dim strPath
Dim p As Object
strpath = "http://www.google.com/images/srpr/logo11w.png"
Set p = ActiveSheet.Shapes.AddPicture(strPath, msoCTrue, msoTrue, 100, 100, 86, 129)
End Sub参考Shapes.AddPicture method:
'Declaration
FunctionAddPicture ( _
Filename As String, _
LinkToFile As MsoTriState, _
SaveWithDocument As MsoTriState, _
LeftAsSingle, _
TopAsSingle, _
WidthAsSingle, _
HeightAsSingle _
) AsShape
Filename: The file from which the OLE object is to be created.
LinkToFile: Microsoft.Office.Core.MsoTriState
The file to link to. Can be one of these MsoTriState constants:
msoCTrue
msoFalse To make the picture an independent copy of the file.
msoTriStateMixed
msoTriStateToggle
msoTrue To link the picture to the file from which it was created.
SaveWithDocument
Type: Microsoft.Office.Core.MsoTriState
Required MsoTriState. To save the picture with the document.LinkToFile和SaveWithDocument不能同时为false,因为无法保存图像。
LinkToFile和SaveWithDocument对可以是:
true, true
true, false
false, true发布于 2017-05-26 17:17:31
我只想补充另一个可能的原因和解决方案。我也得到了错误1004,指定值超出范围。
我启用了对象库和所有这些东西。最后,我意识到在我之前的代码中有一行代码锁定了工作表。解锁后,一切运行正常。
https://stackoverflow.com/questions/20301212
复制相似问题