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()
LVL 1
HolliSttAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

S-TwilleyCommented:
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
0
HolliSttAuthor Commented:
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
0
S-TwilleyCommented:
Ok, i just tested this code (basically same as above) with an excel workbook i just created using MS Office XP:


Imports Excel
Module modExcel
    Sub PrintWorksheet(ByVal Filename As String, ByVal SheetNumber As Integer)
        Dim proceed As Boolean = True

        Dim objExcel As New Excel.Application()
        Dim objWrkBk As Excel.Workbook
        Dim objSht As Excel.Worksheet

        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(SheetNumber)
                    'objSht.PrintOut()
                Catch ex2 As Exception
                    MsgBox(ex2.Message)
                End Try

                objExcel.Quit()
            End If
        End If
    End Sub
End Module


=========

Ok, so i didnt print it out, but no errors were raised.
Just to check, I selected the reference to the "Microsoft Excel 10.0 Object Library" in Solution Explorer... and it's version number is 1.4

I'd say that if the above doesn't work with your workbook... that the work book might be in an old format? or corrupt?   or that your excel library is causing the problems.


=========
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

S-TwilleyCommented:
Sorry... I assumed the code was self explanitory... but to call it in your case:

PrintWorksheet("C:\dwgexcl",1)
0
HolliSttAuthor Commented:
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
0
HolliSttAuthor Commented:
I give you the points because you helped me translating my coding into standard VB.NET.
0
S-TwilleyCommented:
Thanks... glad I could help, even just a little
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.