Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Using Excel with VB.NET give message 32792

Posted on 2005-03-11
7
Medium Priority
?
713 Views
Last Modified: 2010-08-05
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()
0
Comment
Question by:HolliStt
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13522321
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
 
LVL 1

Author Comment

by:HolliStt
ID: 13522379
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
 
LVL 12

Accepted Solution

by:
S-Twilley earned 250 total points
ID: 13522419
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13522421
Sorry... I assumed the code was self explanitory... but to call it in your case:

PrintWorksheet("C:\dwgexcl",1)
0
 
LVL 1

Author Comment

by:HolliStt
ID: 13522464
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
 
LVL 1

Author Comment

by:HolliStt
ID: 13522469
I give you the points because you helped me translating my coding into standard VB.NET.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13522472
Thanks... glad I could help, even just a little
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Loops Section Overview
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question