shapes.addpicture给出1004错误

时间:2013-11-30 14:09:54

标签: excel vba excel-vba

我有一个相当简单的子,但它似乎不起作用:

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”,它可以正常工作并插入图片。根据{{​​3}},第二个真/假会影响图片是否与文档一起保存。我不希望这种情况发生。

任何人都知道为什么会这样?

2 个答案:

答案 0 :(得分:2)

这对我有用:

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

答案 1 :(得分:0)

我只想补充其他可能的原因和解决方案。我也得到了错误1004,指定值超出范围。

我启用了对象库以及所有这些东西。最后我意识到在我的代码中有一行可以锁定工作表。解锁后一切正常。