【问题标题】:shapes.addpicture gives 1004 errorshape.addpicture 给出 1004 错误
【发布时间】:2017-05-26 17:17:31
【问题描述】:

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

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会影响图片是否与文档一起保存。我不希望这种情况发生。

有人知道为什么会这样吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这对我有用:

    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
    

    【讨论】:

    • 我已经找了好几个小时了。您的评论 LinkToFile 和 SaveWithDocument 不能同时为假,因为无处保存图像, 正是我所需要的。
    【解决方案2】:

    我只想添加另一个可能的原因和解决方案。我还收到错误 1004,指定值超出范围。

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

    【讨论】:

      猜你喜欢
      • 2013-04-02
      • 2017-09-30
      • 2014-12-17
      • 2018-12-26
      • 2020-10-08
      • 1970-01-01
      • 2012-08-06
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多