VBA code to insert images in excel sheet

  • Thread starter Thread starter Riya
  • Start date Start date
R

Riya

Hi all
I want a sample code that inserts images in an exce sheet
using VB, and also scales these images to a constant size.
Thanks for help
Regards
Riya
 
'MS Excel object variables
Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objWorkSheet As Excel.Worksheet

Set objExcel = GetObject
("", "excel.application") ' Start the excel COM
Set objWorkBook =
objExcel.Workbooks.Add ' Start a workbook.
objExcel.DisplayAlerts =
False ' Turn off the alerts
Do While objWorkBook.Worksheets.Count >
1 ' keeps only one worksheet in the workbook
Set objWorkSheet = objWorkBook.Worksheets.Item
(objWorkBook.Worksheets.Count)
objWorkSheet.Delete
Loop
Set objWorkSheet =
objExcel.ActiveSheet ' Set objWorksheet to
the remaining worksheet.
objExcel.Visible = True

objWorkSheet.Pictures.Insert
("C:\Pic1.Jpg") ' Insert a picture

'scale picture
objWorkSheet.Pictures("Picture 1").Width = 200
objWorkSheet.Pictures("Picture 1").Height = 200
objWorkSheet.Pictures("Picture 1").Top = 200
objWorkSheet.Pictures("Picture 1").Left = 100
 
'Hope this is what you want

'MS Excel object variables
Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objWorkSheet As Excel.Worksheet

Set objExcel = GetObject
("", "excel.application") ' Start the excel COM
Set objWorkBook =
objExcel.Workbooks.Add ' Start a workbook.
objExcel.DisplayAlerts =
False ' Turn off the alerts
Do While objWorkBook.Worksheets.Count >
1 ' keeps only one worksheet in the workbook
Set objWorkSheet = objWorkBook.Worksheets.Item
(objWorkBook.Worksheets.Count)
objWorkSheet.Delete
Loop
Set objWorkSheet =
objExcel.ActiveSheet ' Set objWorksheet to
the remaining worksheet.
objExcel.Visible = True

objWorkSheet.Pictures.Insert
("C:\Pic1.Jpg") ' Insert a picture

'scale picture
objWorkSheet.Pictures("Picture 1").Width = 200
objWorkSheet.Pictures("Picture 1").Height = 200
objWorkSheet.Pictures("Picture 1").Top = 200
objWorkSheet.Pictures("Picture 1").Left = 100
 
Back
Top