Link to home
Start Free TrialLog in
Avatar of HolliStt
HolliStt

asked on

Using Excel with VB.NET give message 32792

This code gives me a strange message. I have done similar thing without problem on VB6. Why is it a problem in VB.NET. I attached COM Library 1.4 "Microsoft Excel 10.0". However every time I go back the reference disappeard again. It seems I do somehting very basic wrong here.

        Dim filename As String
        Dim objExcel As New Excel.Application
        Dim objWrkBk As Excel.Workbook
        Dim objSht As Excel.Worksheet
        filename = "C:\dwgexcl"
        On Error Resume Next

        objWrkBk = objExcel.Workbooks.Open(filename)
        Debug.WriteLine(Err.Number) ' writes 32792
        Debug.WriteLine(Err.Description) ' Geting a german message on my german XP translated meaning "Old format or wrong type library"
        objSht = objWrkBk.Worksheets(1)
        objSht.PrintOut()
        objExcel.Quit()
Avatar of S-Twilley
S-Twilley

Im not sure about your errors... but I've rewritten your code slightly (although I'm doing this straight here i.e. not tested):

        Dim proceed As Boolean = True
        Dim filename As String
        Dim objExcel As New Excel.Application
        Dim objWrkBk As Excel.Workbook
        Dim objSht As Excel.Worksheet

        filename = "C:\dwgexcl"

        If IO.File.Exists(filename) Then
            Try
                objWrkBk = objExcel.Workbooks.Open(filename)
            Catch ex As Exception
                Msgbox (ex.Message)
                Proceed = False
            End Try

            If Proceed Then
                Try
                    objSht = objWrkBk.Worksheets(1)
                    objSht.PrintOut()
                Catch ex2 As Exception
                    Msgbox(ex2.Message)
                End Try

               objExcel.Quit()
            End If
         End If

============

This may not fix your problem, but I think it should isolate any errors better at least
Avatar of HolliStt

ASKER

Thanks that was very nice. It indeed does not handle the problem. I exhumed my VB6 and tried the following and it works with no problem. Just to prove it. I must admit I have no VB.NET experience. I was on Java and C++ in between. So I have the feeling this is something very very fundamental. Basically. I am saying that I would like to get the bolow code running on VB.NET. Above code from you gets me the already mentioned message. So something is wrong in the connecting of VB.NET with Excel.

    Dim filename As String
    Dim objExcel As New Excel.Application
    filename = "C:\dwgexcl"

    objExcel.Workbooks.Open (filename)
    objExcel.ActivePrinter = "VirtualCreator on Ne01:"
    objExcel.Worksheets(1).PrintOut
    objExcel.Quit

Regards,
Jürgen
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry... I assumed the code was self explanitory... but to call it in your case:

PrintWorksheet("C:\dwgexcl",1)
I found the problem. I am using a German XP (and VB.NET) but have a English Office XP installed. That does not work. Except one does temporarily change the Culture Info. The following worked:

        Dim proceed As Boolean = True
        Dim filename As String
        Dim objExcel As New Excel.Application
        Dim objWrkBk As Excel.Workbook
        Dim objSht As Excel.Worksheet
        Dim thisThread As System.Threading.Thread = _
            System.Threading.Thread.CurrentThread
        Dim originalCulture As System.Globalization.CultureInfo = _
            thisThread.CurrentCulture
        Dim tempExpense As Object
        Dim tempIncome As Object
        filename = "C:\dwgexcl.xls"
        Try
            thisThread.CurrentCulture = New System.Globalization.CultureInfo( _
                "en-US")

        If IO.File.Exists(filename) Then
            Try
                objWrkBk = objExcel.Workbooks.Open(filename)
            Catch ex As Exception
                MsgBox(ex.Message)
                proceed = False
            End Try

            If proceed Then
                Try
                        objSht = objWrkBk.Worksheets(1)
                    objSht.PrintOut()
                Catch ex2 As Exception
                    MsgBox(ex2.Message)
                End Try

                objExcel.Quit()
            End If
        End If

        Finally
            ' Restore the culture information for the thread after the
            ' Excel calls have completed.
            thisThread.CurrentCulture = originalCulture

        End Try

Link in German where it got all explained:

http://msdn.microsoft.com/library/deu/default.asp?url=/library/DEU/dv_wrcore/html/wrconglobalizinglocalizingofficesolutions.asp

Don't know how to find the English version.

Thanks anyhow.

Regards
Jürgen
I give you the points because you helped me translating my coding into standard VB.NET.
Thanks... glad I could help, even just a little