簡體   English   中英

使用VBA在excel中保存圖像

[英]Save image in excel using VBA

我在工作中遇到一種情況,人們必須在excel的某個頁面中手動引入圖片並手動調整其大小。 作為一個完全的初學者,我設法找到了一些 VBA 代碼來幫助通過單擊按鈕並將其插入到特定范圍的單元格中來介紹圖片。 我遇到的問題是我無法弄清楚(在搜索了很多帖子之后)如何正確地引入保存圖像的功能而不提供鏈接,這樣其他人就可以看到報告而不會出現圖片不存在的錯誤.

你能幫我完成這個功能應該在哪里引入嗎?

Private Sub CommandButton3_Click()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
    FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
    Title:="Please select an image...")
If strFileName = "False" Then Exit Sub
Set rngDest = Me.Range("B24:C26")
Set objPic = Me.Pictures.Insert(strFileName)
With objPic
    .ShapeRange.LockAspectRatio = msoFalse
    .Left = rngDest.Left
    .Top = rngDest.Top
    .Width = rngDest.Width
    .Height = rngDest.Height
End With
End Sub

提前致謝!

嘗試這個:

Private Sub CommandButton3_Click()
    Dim strFileName As String
    Dim objPic As Shape '<<<
    Dim rngDest As Range
    strFileName = Application.GetOpenFilename( _
        FileFilter:="Images (*.jpg;*.gif;*.png),*.jpg;*.gif;*.png", _
        Title:="Please select an image...")
    If strFileName = "False" Then Exit Sub
    Set rngDest = Me.Range("B24:C26")

     Set objPic = Me.Shapes.AddPicture(Filename:=strFileName, _
                                       linktofile:=msoFalse, _
                                       savewithdocument:=msoCTrue, _
                                       Left:=rngDest.Left, Top:=rngDest.Top, _
                                       Width:=rngDest.Width, Height:=rngDest.Height)

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM