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(fi lename)
Debug.WriteLine(Err.Number ) ' writes 32792
Debug.WriteLine(Err.Descri ption) ' Geting a german message on my german XP translated meaning "Old format or wrong type library"
objSht = objWrkBk.Worksheets(1)
objSht.PrintOut()
objExcel.Quit()
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(fi
Debug.WriteLine(Err.Number
Debug.WriteLine(Err.Descri
objSht = objWrkBk.Worksheets(1)
objSht.PrintOut()
objExcel.Quit()
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).Pri ntOut
objExcel.Quit
Regards,
Jürgen
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).Pri
objExcel.Quit
Regards,
Jürgen
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry... I assumed the code was self explanitory... but to call it in your case:
PrintWorksheet("C:\dwgexcl ",1)
PrintWorksheet("C:\dwgexcl
ASKER
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.Cu rrentThrea d
Dim originalCulture As System.Globalization.Cultu reInfo = _
thisThread.CurrentCulture
Dim tempExpense As Object
Dim tempIncome As Object
filename = "C:\dwgexcl.xls"
Try
thisThread.CurrentCulture = New System.Globalization.Cultu reInfo( _
"en-US")
If IO.File.Exists(filename) Then
Try
objWrkBk = objExcel.Workbooks.Open(fi lename)
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
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.Cu
Dim originalCulture As System.Globalization.Cultu
thisThread.CurrentCulture
Dim tempExpense As Object
Dim tempIncome As Object
filename = "C:\dwgexcl.xls"
Try
thisThread.CurrentCulture = New System.Globalization.Cultu
"en-US")
If IO.File.Exists(filename) Then
Try
objWrkBk = objExcel.Workbooks.Open(fi
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
ASKER
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
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(fi
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