2012-08-07 112 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