Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
insert image in cell of excel by using vb.net code
VB
Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object



        oExcel = CreateObject("Excel.Application")
        oExcel.Visible = True
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)
        oSheet.Shapes.AddPicture("C:\Sample Pictures\p.jpg", False, True, 0, 0, 45, 50) 


this is the half code trough which we insert image in excel, bt it not fit to the one cell of excel................

pls help me with this..............
Posted
Updated 24-Aug-16 12:13pm
v3

The Range[^] Top and Left properties are used to define the top left corner.

Width and Height should be in points instead of pixels. Use the 3/4 multiplier.

You may scale your pictures also.

Here is a short example in VBA:
VB
Dim ws As Worksheet
Dim r As Range
Dim fileName As String
Dim pictureWidth As Integer
Dim pictureHeight As Integer
Dim shape As shape

Set ws = Worksheets(1)

Set r = ws.Cells(5, 2)

fileName = "d:\picture.png"

pictureWidth = 195
pictureHeight = 102

Set shape = ws.Shapes.AddPicture(fileName, msoTrue, msoTrue, r.Left, r.Top, pictureWidth * 3 / 4, pictureHeight * 3 / 4)

shape.LockAspectRatio = msoTrue
shape.ScaleHeight 2, msoTrue, msoScaleFromTopLeft
 
Share this answer
 
Thank u for response, bt it shows following errors, i hv also used 'Imports Microsoft.Office.Interop.Excel' namespace, still its getting ths errors

Error 1 :'worksheet' is ambiguous in the namespace Microsoft.Office.Interop.Excel

Error 2 : 'System.Data.Range' is not accessible in this context because it is 'Friend'.
Error 3 : 'msoTrue' is not declared. It may be inaccessible due to its protection level.
 
Share this answer
 
thank u guys for ur respose.....
i solved ths by myself
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900