![Excel VBA: Fill.UserPicture](https://rvso.com/image/1476406/Excel%20VBA%3A%20Fill.UserPicture.png)
cell.Comment.ShapeRange...
「実行時エラー '1004': アプリケーション定義またはオブジェクト定義エラー」というエラーが表示されます。何が問題なのでしょうか?
Cells(cell.Row, 6)
画像のURLを含む参照列です。例:http://somelink.com/img.jpg
Sub test()
Dim rng As Range
Set rng = Range("B2:B2331")
For Each cell In rng.Cells
cell.AddComment
cell.Comment.Text Text:="Owner:" & Chr(10) & ""
cell.Comment.ShapeRange.Fill.UserPicture Cells(cell.Row, 6).Value
Next
End Sub
答え1
まず、.shaperange
は のプロパティまたはメソッドではないのでcomment
、使用できません。
うまくいく例を挙げます -
Sub tete()
Dim rng As Range
Set rng = ActiveSheet.Cells(5, 6)
rng.AddComment
rng.Comment.Text Text:="hi"
rng.Comment.Shape.Fill.UserPicture ("C:\Users\path\to\pic.jpg")
End Sub
ここで、パスを文字列として必要とする の使用法について説明します.UserPicture()
。したがって、パスがそのセル内にある場合は、その値を取得するようにしてください。
つまり、セルG1 =C:\Users\path\to\pic.jpg
rng.Comment.Shape.Fill.UserPicture (Range("G1"))
動作するでしょう。
これは編集です歴史スタンプこれは、回答の本来の意図を変えず、それに大きく基づいているため、新しい回答としてあまりうまく機能しなかったり、目立たなかったりする可能性があるため、許可しています。
このバージョンは、Excel 2011 を搭載した macOS 10.10.5 で動作しました。
Sub tete()
' Raystafarian
' https://superuser.com/a/1011255/2638314
' This is the shortest code I have seen for inserting a picture into
' a commnet. & It works.
Dim rng As Range
Dim aPicture As String
Debug.Print "-------------------------- " & Now
aPicture = "Macintosh SSD:Users:mac:Desktop:numbers:1.png"
Set rng = ActiveSheet.Cells(5, 8)
rng.AddComment
rng.Comment.Text Text:=" "
Debug.Print aPicture
rng.Comment.Shape.Fill.UserPicture (aPicture)
End Sub