
在 Excel 中,假設我選擇一個圖片(例如 test.jpeg)作為評論的背景。我希望評論框採用 test.jpeg 的尺寸。由於我計劃有數百條這樣的評論,所以我的問題是:有沒有辦法自動執行此操作?
答案1
我修補了一個 VBA 巨集。使用ALT+開啟 VBA 編輯器F11並將以下程式碼貼到表1。使用ALT+執行巨集F8
宏的作用
詢問使用者要插入的圖片的路徑(可以是 jpg、bmp 或 png)
將該圖片載入為
WIA.imageFile
。稍後我們使用objImage.Height
和objImage.Width
來獲取真實的圖片尺寸。這種方法是我發現的獲取像素尺寸的最短方法(其他 VBA 方法可能會給你緹。它們是一個可怕的發明)
如果目前選取的儲存格中不存在舊註釋,則插入空白註釋
將所選圖片設定為評論形狀的背景圖片
調整評論形狀尺寸,讓圖片完美貼合
Sub InsertComment()
Dim strImagePath As Variant
Dim objImage As Object
strImagePath = Application.GetOpenFilename("Picture, *.jpg; *.png; *.bmp")
If strImagePath = False Then Exit Sub
Set objImage = CreateObject("WIA.ImageFile")
objImage.LoadFile strImagePath
With ActiveCell
If .Comment Is Nothing Then .AddComment ("")
.Comment.Shape.Fill.UserPicture strImagePath
.Comment.Shape.Height = objImage.Height * 0.75
.Comment.Shape.Width = objImage.Width * 0.75
End With
End Sub