vba excel图形

vba excel图形,excel,vba,macos,excel-2011,Excel,Vba,Macos,Excel 2011,我使用了一个小的子程序,通过 ActiveSheet.Pictures.Insert(URL).Select 这适用于Excel 2003(Windows),但不再适用于Excel 2011(Mac) 因此,我修改了我的子程序 (一如建议),, 但是子程序在 theShape.Fill.UserPicture URL 显示错误消息 “-2147024894(80070002)Fehler der Methode用户图片对象填充格式” 矩形是绿色的 Sub Q1() Dim wks As

我使用了一个小的子程序,通过

ActiveSheet.Pictures.Insert(URL).Select
这适用于Excel 2003(Windows),但不再适用于Excel 2011(Mac)

因此,我修改了我的子程序 (一如建议),, 但是子程序在

theShape.Fill.UserPicture URL 
显示错误消息

“-2147024894(80070002)Fehler der Methode用户图片对象填充格式”

矩形是绿色的

Sub Q1()

Dim wks As Worksheet
Dim URL As String
Dim i As Long
Dim lastRow As Long
Dim theShape As Shape
Dim pasteCell As Range

' Used Worksheet
Set wks = Worksheets("Blatt1")

' Delete already existing shapes
For Each theShape In wks.Shapes
        theShape.Delete
Next theShape

' Check all existing rows in Column K
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
For i = 2 To lastRow

' the URLs are already computed and stored in column K
URL = wks.Range("K" & i).Value

' try to put the images in column L
Set pasteCell = wks.Range("L" & i)
pasteCell.Select

' Create a Shape for putting the Image into
' ActiveSheet.Pictures.Insert(URL).Select is deprecated and does not work any more!!!
Set theShape = wks.Shapes.AddShape(msoShapeRectangle, pasteCell.Left, pasteCell.Top, 200, 200)

' fill the shape with the image after greening
theShape.Fill.BackColor.RGB = RGB(0, 255, 0)
theShape.Fill.UserPicture URL

Next i

End Sub

有什么建议或提示吗?可能我是个盲人….

您是否尝试过将形状设置为URL的语法:

Sub Picadder()

Dim Pic As Shape

Set Pic = ActiveSheet.Shapes.AddPicture("http://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png", msoFalse, msoTrue, 0, 0, 100, 100)

End Sub
此代码在适应您的工作时,可能会与以下内容类似:

Sub Q1()

Dim wks As Worksheet
Dim URL As String
Dim i As Long
Dim lastRow As Long
Dim theShape As Shape
Dim pasteCell As Range

' Used Worksheet
Set wks = Worksheets("Blatt1")

' Delete already existing shapes
For Each theShape In wks.Shapes
        theShape.Delete
Next theShape

' Check all existing rows in Column K
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
For i = 2 To lastRow

' the URLs are already computed and stored in column K
URL = wks.Range("K" & i).Value

' try to put the images in column L
Set pasteCell = wks.Range("L" & i)
pasteCell.Select

' Create a Shape for putting the Image into
' ActiveSheet.Pictures.Insert(URL).Select is deprecated and does not work any more!!!
Set theShape = wks.Shapes.AddPicture(URL, pasteCell.Left, pasteCell.Top, 200, 200)

' Set shape image backcolor.
theShape.Fill.BackColor.RGB = RGB(0, 255, 0)

Next i

End Sub
您的URL将需要正确格式化-我必须在我的URL上使用引号作为初始代码段,以使其有效运行,但这可能是一个解决方案

对于MacExcel2011,在他的博客上讨论了一个变通方法。显然,在MacExcel2011中,要将图像与单元格联系起来并不容易。此外,研究表明,在excel工作簿中插入图像的问题已被多次提出。而且,到目前为止,在研究中,似乎还没有通过图片方法很容易地解决这个问题。因此,变通解决方案可能是最好的解决方案

这段代码片段是从Michael的博客中改编和移植的,如下所示:

Function InsertImageCommentAsWorkAround(title As String, cellAddress As Range)

    ' Define variables used in the comment.
    Dim ImageCommentContainer As comment

  ' Clear any existing comments before adding new ones.
  Application.ActiveCell.ClearComments

  ' Define the comment as a local variable and assign the file name from the _
  ' _ cellAddress as an input parameter to the comment of a cell at its cellAddress.

  ' Add a comment.
  Set ImageCommentContainer = Application.ActiveCell.AddComment

  ' With the comment, set parameters.
  With ImageCommentContainer
    .Text Text:=""

        'With the shape overlaying the comment, set parameters.
        With .Shape
          .Fill.UserPicture (cellAddress.Value)
          .ScaleHeight 3#, msoFalse, msoScaleFormTopLeft
          .ScaleWidth 2.4, msoFalse, msoScaleFromTopLeft
        End With

  End With

  InsertImageCommentAsWorkAround = title

End Function

我建议将注释集调整到循环中,并使用它将图像设置到位,使用循环中的形状格式设置由调整后的代码生成的注释形状的格式。

您是否确保所引用的URL实际上是图像?我使用的是Excel2010(Windows),但你的子系统运行良好(我使用了一个谷歌地图URL示例)。我将查看
URL
的值。您可能有一个格式错误的链接。谢谢您的提示。但是URL是可以的。我也使用谷歌API,正如我所提到的,它与Excel2003一起工作。我还没有更改我工作表的内容。我用一个给定的URL测试了UserPicture方法,但它不起作用。目前,我认为问题可能取决于我的MS Office设置。有经验吗?URL和本地文件访问之间存在差异:
theShape.Fill.UserPicture“http://maps.googleapis.com/maps/api/streetview?_&location=51.501554,-0.178082&size=512x512&heading=135&fov=100&pitch=0&sensor=false“
以错误
theShape.Fill.UserPicture结束“Macintosh HD:Library:User Pictures:Flowers:Flower.tif”
works!!@TomThumb所以,我认为URL和本地文件访问之间的差异是问题所在?你的解决方案是什么?@JackorangelTern抱歉,我没有解决方案……我需要URL访问!同时我用Excel 2007(Windows)测试了代码它成功了!我现在有点困惑…我已经尝试过了,但在尝试添加图片时出现了“运行时错误1004-找不到文件”。这发生在堆栈溢出URL以及使用Excel:mac 2011(VBA版本14.0.0)测试时?Tom,我将对VBA版本进行一些研究-我在Excel 2007上进行了测试,没有经过思考-您已经以这种方式运行了=)。Tom,在研究之后,我发现许多其他人都有这个问题;)我没有找到图片方法解决方案,这是最佳的,但我找到了Michael McLaughlin的解决方案,我已经移植了clos请直接访问我的SO帖子。看看这是否有效,并让我知道结果。我不敢相信:它适用于Excel:mac 2011!!!我修改了与MacLochlainns博客相对应的代码:
函数InsertCommentImage(标题为字符串,URL为字符串)…ImageCommentContainer.Shape.Fill.UserPicture(URL)
URL中的图片出现了!!!太好了!!非常感谢!!没问题=)您还应该知道Excel mac 2011也有,它出现了,所以在创新这些kewl新功能时要小心。祝您好运!