Excel VBA:填滿.UserPicture

Excel VBA:填滿.UserPicture

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"))

會工作。


這是由歷史印記我允許這樣做,因為它不會改變答案的原始意圖,並且因為它很大程度上基於它,所以它可能不會做得太好或作為新答案具有可見性。

此版本適用於 macOS 10.10.5 和 Excel 2011。

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

相關內容