Problem bei Typumwandlung (System._ComObject > ..Excel.Application)

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von petaod.

    Problem bei Typumwandlung (System._ComObject > ..Excel.Application)

    Hallo,

    bei folgendem Code (dient dem Import und Bearbeitung einer Textdatei) triit bei "xlBook = xlApp.Workbooks.Open(sLocation)" (z.24) diese Fehlermeldung auf :

    An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication3.exe
    Additional information: Das COM-Objekt des Typs "System.__ComObject" kann nicht in den Schnittstellentyp "Microsoft.Office.Interop.Excel.Application" umgewandelt werden.

    Quellcode

    1. Imports Excel = Microsoft.Office.Interop.Excel
    2. Public Class Form1
    3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4. Dim lngINet As Int32
    5. Dim lngINetConn As System.IntPtr
    6. Dim blnRC As Boolean
    7. Dim dPath As String
    8. Dim sLocation As String
    9. Dim xlApp As New Excel.Application
    10. Dim xlBook As Excel.Application
    11. Dim xlSheet As Excel.Application
    12. 'Hier Download und Speicherort angeben
    13. dPath = "dPfad"
    14. sLocation = "sPfad" & Replace(dPath, "zu_entfernender_String", "")
    15. lngINet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)
    16. lngINetConn = InternetConnect(lngINet, "ip", 0, "username", "password", 1, 0, 0)
    17. blnRC = FtpGetFile(lngINetConn, dPath, sLocation, 0, 0, 1, 0)
    18. InternetCloseHandle(lngINetConn)
    19. InternetCloseHandle(lngINet)
    20. xlBook = xlApp.Workbooks.Open(sLocation)
    21. xlSheet = xlBook.Sheets(1)
    22. xlApp.Visible = True
    23. With xlSheet
    24. .Columns(1).TextoColums(
    25. Destination:= .Cells(6, 1),
    26. Datatype:=Excel.XlTextParsingType.xlDelimited,
    27. Textqualifier:=Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
    28. Semicolon:=True,
    29. DecimalSeparator:=".")
    30. End With
    31. End Sub
    32. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (
    33. ByVal sAgent As String,
    34. ByVal lAccessType As Int32,
    35. ByVal sProxyName As String,
    36. ByVal sProxyBypass As String,
    37. ByVal lFlags As Integer) As Int32
    38. Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (
    39. ByVal hInternetSession As System.IntPtr, ByVal sServerName As String,
    40. ByVal nServerPort As Integer, ByVal sUsername As String,
    41. ByVal sPassword As String, ByVal lService As Int32,
    42. ByVal lFlags As Int32, ByVal lContext As System.IntPtr) As System.IntPtr
    43. Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (
    44. ByVal hFtpSession As IntPtr, ByVal RemoteFile As String,
    45. ByVal NewFile As String, ByVal fFailIfExists As Boolean,
    46. ByVal dwFlagsAndAttributes As Integer, ByVal Flags As Integer,
    47. ByVal Context As IntPtr) As Boolean
    48. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As IntPtr) As Boolean
    49. End Class


    Den Verweis auf Microsoft.Office.Interop.Excel habe ich gesetzt. Die Datei wird tatsächlich runtergeladen.
    Ich habe gelesen, dass dieser Fehler durch up/downgrades des office auftreten kann. Es ist nur so, dass ich auf meinem Arbeitrechner schon alle FIixes dafür ausprobiert habe. Und auf meinem Rechner zu hause funktioniert der Code auch nicht. Ich bin auch ein Anfänger, kann gut sein, dass der Code fehlerhaft ist.

    Würde mich freuen, wenn jemand mir da weiterhelfen kann.
    Vielen Dank.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „summertimerainman“ ()

    schau in deinen Code: Welchen Datentyps ist xlBook, und ist eine solche Variable geeignet, dass ihr ein (geöffnetes) Workbook zugewiesen wird?
    Denn das macht ja wohl xlApp.Workbooks.Open(sLocation): Es erstellt ein Workbook-Objekt.

    Also - gugge dein Code, zeile#10: Welchen Datentyp hat xlBook, und kann man dem ein Workbook zuweisen?