温馨提示:本文翻译自stackoverflow.com,查看原文请点击:image - How to insert a picture into Excel at a specified cell position with VBA
excel image insert vba

image - 如何使用VBA在指定单元格位置处将图片插入Excel中

发布于 2020-04-05 00:48:33

我使用以下代码将“ .jpg”文件添加到我的Excel工作表中:

'Add picture to excel
xlApp.Cells(i, 20).Select
xlApp.ActiveSheet.Pictures.Insert(picPath).Select
'Calgulate new picture size
With xlApp.Selection.ShapeRange
    .LockAspectRatio = msoTrue
    .Width = 75
    .Height = 100
End With
'Resize and make printable
With xlApp.Selection
    .Placement = 1 'xlMoveAndSize
    '.Placement = 2 'xlMove
    '.Placement = 3 'xlFreeFloating
    .PrintObject = True
End With

我不知道自己在做什么错,但是没有将其插入正确的单元格中,所以该怎么办才能将该图片放入Excel中的指定单元格中?

查看更多

提问者
Berker Yüceer
被浏览
122
SWa 2012-10-17 22:42

尝试这个:

With xlApp.ActiveSheet.Pictures.Insert(PicPath)
    With .ShapeRange
        .LockAspectRatio = msoTrue
        .Width = 75
        .Height = 100
    End With
    .Left = xlApp.ActiveSheet.Cells(i, 20).Left
    .Top = xlApp.ActiveSheet.Cells(i, 20).Top
    .Placement = 1
    .PrintObject = True
End With

最好不要在Excel中选择任何内容,通常这从来没有必要,这会使代码变慢。