3

小さなサブルーチンを使用して、シートに画像を挿入しました

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 Objects 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

提案やヒントはありますか?おそらく私はコウモリのように盲目です....

4

1 に答える 1

2

形状を 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 で引用符を使用する必要がありましたが、それが解決策になる可能性があります。

Mac-Excel 2011 については、Michael McLaughlinが彼のブログで説明している回避策があります。明らかに、Mac-Excel 2011 で画像をセルに結び付けるのは、仮にあったとしても簡単ではありません。さらに、調査によると、画像を 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

コメント セットをループに適応させ、それを使用して画像を配置し、ループ内のシェイプ フォーマットを使用して、適応されたコードによって生成されたコメント シェイプのフォーマットを設定することをお勧めします。

于 2012-08-29T14:44:01.393 に答える