+ Reply to Thread
Results 1 to 3 of 3

Thread: embedding a excel attachment using vb.net

  1. #1
    niklesh is offline VB.NET Forum Newbie niklesh is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2009
    Posts
    1
    Reputation
    0

    Exclamation embedding a excel attachment using vb.net

    Hi,

    I'm providing a link button in my page(which contains a datagrid) so that when the user clicks on it they will be able to see the contents of the datagrid in a separate excel file.I provide the user with the option of attaching documents while they use my application.I would want that particular attachment to be embedded into excel document along with other contents.Is there a way to do this??

    Note:I don't want to specify the link where the attachment is present.Instead I would need the attached document to be embedded into the excel file.

  2. #2
    Tom
    Tom is offline VB.NET Forum Idol Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET Tom master of VB.NET
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2005
    Posts
    746
    Reputation
    340

    Default

    The contents of a datagrid (which is actually the contents of a datatable) can be output and written to an Excel file using Oledb with no need of automating Excel.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    10,843
    Reputation
    1443

    Default

    The easiest way to find such code is to start "record macro" in Excel and do the action manually, then look at the VBA code, the generated code will be much like the code you have to write in VB when automating the Office objects, at least you can in most cases figure out the "trick" about how it is done. In this case inserting an object from file into the Excel book will generate code like this:
    Code:
    ActiveSheet.OLEObjects.Add(filename and some arguments...)
    Automating Excel from VB is pretty basic, add the reference to the Excel object library, import the namespace for easier coding, create Excel application object, create/open the book, manipulate it and close down. Here's a sample that does that, since I use Option Strict I do some type casts, but notice the similarities with the VBA code.
    Code:
    Imports Excel = Microsoft.Office.Interop.Excel
    Code:
    Dim app As New Excel.Application
    Dim book As Excel.Workbook = app.Workbooks.Add
    Dim sheet As Excel.Worksheet = CType(book.ActiveSheet, Excel.Worksheet)
    Dim oleobj As Excel.OLEObjects = CType(sheet.OLEObjects, Excel.OLEObjects)
    oleobj.Add(Link:=False, DisplayAsIcon:=False, Filename:="D:\path\Word.doc")
    book.SaveAs("D:\path\Excel.xls")
    book.Close()
    app.Quit()
    If you're a "late-binder" then you can reduce three of the code lines and the casting to this:
    Code:
    book.ActiveSheet.OLEObjects.Add(..same params)
    That's really close to the starting point, right?

    Despite the naming I don't think you can add Ole objects with a OleDB data export, usually it is only for tabular string/numeric data and none of the formatting or application specific features.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Tags for this Thread

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