+ Reply to Thread
Results 1 to 3 of 3

Import an Image into Excel using VB.Net and Option Strict On

  1. #1
    Jedawi
    Guest

    Import an Image into Excel using VB.Net and Option Strict On

    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.ReleaseComObject(oExcel)

    oSheet = Nothing
    oBook = Nothing
    oExcel = Nothing

    Any help would be much appreciated.

    Regards




  2. #2
    NickHK
    Guest

    Re: Import an Image into Excel using VB.Net and Option Strict On

    Jedawi,
    I am no .Net expert, but as ActiveSheet could in theory be a worksheet,
    chart or macro, it has to evaluate to an "Object".
    Does
    oSheet.Pictures.Insert("C:\Images\AB1.jpg").Select()
    work ?

    NickHK

    "Jedawi" <[email protected]> wrote in message
    news:[email protected]...
    > 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.ReleaseComObject(oExcel)
    >
    > oSheet = Nothing
    > oBook = Nothing
    > oExcel = Nothing
    >
    > Any help would be much appreciated.
    >
    > Regards
    >
    >
    >




  3. #3
    Registered User
    Join Date
    07-02-2013
    Location
    california
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Import an Image into Excel using VB.Net and Option Strict On

    Hi, Jedawi
    As a newbie both in vb.net and forum, my suggestions may not work.
    But i'll try my best.
    As far as i know, with the code below, it's practical to import image using VB.net
    Please Login or Register  to view this content.
    This code is for an image in png form.
    Hope it works.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1