2017-06-06 77 views
2

試圖編寫一個VBA excel宏,這將允許我將圖片作爲鼠標懸停在單元格上的彈出窗口插入。在Excel VBA中相對於填充圖片的原始大小更改評論大小

我通過在單元格中插入註釋並將註釋的填充設置爲指定圖片來完成此操作。

我想圖片保持其原有的縮放

設置註釋使用的圖片作爲填充背景後,我可以手動右鍵單擊該單元格,單擊編輯註釋,用鼠標右鍵單擊註釋,進入「尺寸」選項卡,選擇「相對於原始圖片尺寸」複選框,並將尺寸高度和尺寸設置爲100%,從而達到預期的效果,如下所示:enter image description here

錄製宏, VBA複製這是沒有記錄的結果。

一個錯誤使用targetComment.Shape.ScaleHeight 1, msoTrue結果:

Run-time error '-2147024891 (80070005)': 
The RelativeToOriginalSize argument applies only to a picture or an OLE object 

下面是VBA代碼的截圖,產生這個錯誤:

enter image description here

有誰知道如何訪問是什麼通過VBA的對話框?

+1

我會使用超鏈接,而不是評論:http://optionexplicitvba.blogspot.co.uk/2011/04/rollover-b8-ov1.html,那麼你可以使用目標arguement接入小區屬性,如高度 – Absinthe

+0

@Absinthe我不認爲這對我有用,因爲我想要顯示的圖片被錨定/生活在一個形狀而不是一個單元格中(這樣,相鄰的單元格可以是常規大小,即使它們在鼠標懸停上顯示大圖)。我也不能在我的用例中使用相鄰的單元格(它們是表格的一部分) – macdonaldtomw

回答

1

使用評論來顯示縮放圖像可以完成。訣竅是自己計算比例因子並將其應用於圖像。我已使用Windows Image Acquisition Automation Layer訪問image file's dimensions

下面的示例訪問我的Temp目錄中的JPG圖像,並使用適當的縮放比例將其添加到單元格的註釋中。

Option Explicit 

Sub test() 
    '--- delete any existing comment just for testing 
    If Not Range("C5").Comment Is Nothing Then 
     Range("C5").Comment.Delete 
    End If 
    InsertCommentWithImage Range("C5"), "C:\Temp\laptop.jpg", 1# 
End Sub 

Sub InsertCommentWithImage(imgCell As Range, _ 
          imgPath As String, _ 
          imgScale As Double) 
    '--- first check if the image file exists in the 
    ' specified path 
    If Dir(imgPath) <> vbNullString Then 
     If imgCell.Comment Is Nothing Then 
      imgCell.AddComment 
     End If 

     '--- establish a Windows Image Acquisition Automation object 
     ' to get the image's dimensions 
     Dim imageObj As Object 
     Set imageObj = CreateObject("WIA.ImageFile") 
     imageObj.LoadFile (imgPath) 

     Dim width As Long 
     Dim height As Long 
     width = imageObj.width 
     height = imageObj.height 

     '--- simple scaling that keeps the image's 
     ' original aspect ratio 
     With imgCell.Comment 
      .Shape.Fill.UserPicture imgPath 
      .Shape.height = height * imgScale 
      .Shape.width = width * imgScale 
     End With 
    End If 
End Sub 
+0

有趣的解決方案,我不知道WIA實用程序。好一個!我想更多的跨平臺友好的解決方案是禁用screenupdating,將圖片插入圖片形狀,查詢其尺寸,然後刪除它,然後按照示例中的步驟手動設置註釋 – macdonaldtomw

+1

的Shape.height和Shape.width參數如果跨平臺是目標的一部分,那麼是的添加(屏幕禁用)形狀肯定會讓你的尺寸。關鍵是所有的縮放都是自己完成的,而不是使用'ScaleHeight'。因此,保持原始寬高比的決定也取決於您。 – PeterT

相關問題