Problem opening/editing a Excel Document using interops


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Problem opening/editing a Excel Document using interops

  1. #1
    Join Date
    Mar 2006
    Posts
    100

    Problem opening/editing a Excel Document using interops

    I am supposed to open an excel document and edit it then close it and use the inputed data to fill a Mail Merge on a word Document. I have it working, however, it will work only once and then it will throw an error because excel does not close. After I close excel using the task manager it works again. Any ideas? A simplified version (that is still not working) of what I do is this...

    Code:
    Imports Excel = Microsoft.Office.Interop.Excel
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim excelApp As New Excel.Application
            Try
                excelApp.Workbooks.Open("C:\Program Files\DISClient\Templates\TemplateMerge.xls")
                excelApp.Workbooks.Close()
            Catch ex As Exception
                MsgBox(ex)
            Finally
                excelApp.Quit()
                excelApp = Nothing
            End Try
        End Sub
    End Class

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Mar 2006
    Posts
    100
    Thanks Phil,

    That helped a little (Actually I came across something similar on the MS forums) but now I am having issues with the Excel.Workbooks object. I couldn't figure out why it wouldn't close after I opened a workbook, my originla code was
    Code:
    Imports Excel = Microsoft.Office.Interop.Excel
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim excelApp As New Excel.Application
            Dim excelBook As object
            Try
    
                excelBook = excelApp.Workbooks.Open("C:\Program Files\DISClient\Templates\TemplateMerge.xls")
            Catch ex As Exception
                MsgBox(ex)
            Finally
                excelBook.Close(False)
                NAR(excelBook)
                excelApp.Quit()
                NAR(excelApp)
            End Try
        End Sub
        Private Sub NAR(ByVal o As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
            Catch ex As Exception
            Finally
                o = Nothing
            End Try
        End Sub
    End Class
    and it woked but it did not close so I tried this to change the variable to Dim excelBook As New Excel.Workbook and now I am getting a COM error...

    Code:
    Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154.
    Anyone know of any outstanding issues? In the meantime I'll try to scour MSDN.

  4. #4
    Join Date
    Mar 2006
    Posts
    100
    Ok figured the last part out too the variable should be

    Dim excelBook As Excel.Workbook

    new messes it up but it still doesn't close :-(.

  5. #5
    Join Date
    Mar 2006
    Posts
    100
    Got it needed to add Gc.collect

    I guess the automatic GC doesn't work well with these

Similar Threads

  1. xsl document function with xml returned from asp
    By Andrew \(BTClick\) in forum XML
    Replies: 1
    Last Post: 11-08-2002, 07:15 PM
  2. Document formatting with DHTML and IE 5.5
    By Robert Giffin in forum Web
    Replies: 0
    Last Post: 07-18-2001, 07:26 PM
  3. Permission problem saving an xml document locally
    By Neil Giarratana in forum XML
    Replies: 1
    Last Post: 06-22-2001, 08:32 AM
  4. Replies: 1
    Last Post: 01-23-2001, 04:07 AM
  5. Replies: 0
    Last Post: 08-23-2000, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center