By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,874 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,874 IT Pros & Developers. It's quick & easy.

Import an Image into Excel with Option Strict On

P: n/a
Hi,

would anyone be able to point me in the right direction on how to import an
image (.jpg file) into
an Excel worksheet using VB.Net and Option Strict On. This should be
straight forward however with Excel returning some weak-typed objects we are
having problems.

The only problem we have is with line (full code below)
oExcel.ActiveSheet.Pictures.Insert("C:\Images\AB1. jpg").Select()

Option Strict disallows late binding and oExcel.Activesheet returns an
Object Type of Object

With Option Strict off the following code works fine:

'Start Excel and get Application object
Dim oExcel As Excel.Application = New Excel.Application

'Get a new workbook
Dim oBook As Excel.Workbook = CType(oExcel.Workbooks.Add(), Excel.Workbook)
Dim oSheet As Excel.Worksheet = CType(oBook.ActiveSheet, Excel.Worksheet)
Dim oRng As Excel.Range

With oSheet
CType(.Cells(4, 1), Excel.Range).Value = "COMPILED:-"
CType(.Cells(5, 1), Excel.Range).Value = "TITLE:-"
CType(.Cells(6, 1), Excel.Range).Value = "APPROVED"
CType(.Cells(27, 2), Excel.Range).Value = "PIC ID"
CType(.Cells(24, 8), Excel.Range).Value = "PIC ID"
CType(.Cells(45, 7), Excel.Range).Value = "PIC ID"
oRng = DirectCast(.Cells(5, 5), Excel.Range)
oRng.Cells.Select()
End With

oExcel.ActiveSheet.Pictures.Insert("C:\Images\AB1. jpg").Select()
oSheet.SaveAs(tempFileName)

'Save the report.
oExcel.Workbooks.Close()
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComO bject(oExcel)

oSheet = Nothing
oBook = Nothing
oExcel = Nothing

Any help would be much appreciated.

Regards
Dec 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.