我使用以下代码将“ .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中的指定单元格中?
尝试这个:
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中选择任何内容,通常这从来没有必要,这会使代码变慢。
对于在这里着陆的人。不同版本的Excel处理此请求的方式有所不同,Excel 2007将图片插入为对象,即将其嵌入到工作簿中。Excel 2010将其插入为链接,如果您打算将其发送给任何人,这将是糟糕的时机。您需要更改插入以指定它是嵌入式的:Insert(Filename:= <path>,LinkToFile:= False,SaveWithDocument:= True)