2012-08-07 114 views
3

我用一个小的子程序通过VBA擅长形状

ActiveSheet.Pictures.Insert(URL).Select 

能正常工作与Excel 2003(Windows)中插入图片到我的表,但不与Excel 2011(苹果机)工作了。

所以我修改 (如提出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 

任何建议或提示?也许我是盲目的蝙蝠....

+0

你确定你所引用的URL实际上是一个图片吗?我使用的是Excel 2010(Windows),但是您的子工作正常(我使用了一个示例Google Maps URL)。我会看看'URL'的值。您可能有畸形的链接。 – Zairja 2012-08-07 18:58:48

+0

感谢您的提示。但是URL是好的。我也使用Google API,正如我所提到的,这适用于Excel 2003.我没有更改我的工作表内容。我用给定的URL测试了UserPicture-method,它不起作用。 目前我认为这个问题可能取决于我的MS-Office的设置。任何经验? – TomThumb 2012-08-08 06:10:47

+1

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:库:用户图片:鲜花:花。tif“' 作品!! – TomThumb 2012-08-08 07:22:12

回答

1

你试过沿此线语法的形状设置为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中使用的报价为初始片段有效地得到它的功能,但它可能是一个解决办法。

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

的代码片段,这是非常密切的适应和迈克尔的博客移植,如下:

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 

我会建议调整的评论集到你的循环,并用它来设置你的图像到位,使用循环中的形状格式设置由适应代码生成的注释形状的格式。

+0

我已经试过这个,但我得到了“运行时错误1004 - 文件无法找到”whe ñ试图添加图片。 这种情况与您的堆栈溢出,URL以及与http://maps.googleapis.com/maps/api/streetview?_&location=51.501554,-0.178082&size=512x512&heading=135&fov=100&pitch=0&sensor=false 你使用Excel:mac 2011(VBA版本14.0.0)进行测试? – TomThumb 2012-08-30 07:33:29

+0

汤姆,我会对这个VBA版本做一些研究 - 我在Excel 2007上没有经过思考的测试 - 而且你已经得到了它的工作方式=)。 – JackOrangeLantern 2012-08-30 14:14:41

+0

Tom在研究后发现许多其他人都分享了这个问题;)我没有找到一种图片方法解决方案,它是最优化的,但是我找到了迈克尔麦克劳克林的解决方法,我已经将其密切并直接移植到了我的SO贴子。看看是否可以解决问题,并让我知道结果。 – JackOrangeLantern 2012-08-30 15:36:59