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.
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.
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
- Imports Excel = Microsoft.Office.Interop.Excel
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim lngINet As Int32
- Dim lngINetConn As System.IntPtr
- Dim blnRC As Boolean
- Dim dPath As String
- Dim sLocation As String
- Dim xlApp As New Excel.Application
- Dim xlBook As Excel.Application
- Dim xlSheet As Excel.Application
- 'Hier Download und Speicherort angeben
- dPath = "dPfad"
- sLocation = "sPfad" & Replace(dPath, "zu_entfernender_String", "")
- lngINet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)
- lngINetConn = InternetConnect(lngINet, "ip", 0, "username", "password", 1, 0, 0)
- blnRC = FtpGetFile(lngINetConn, dPath, sLocation, 0, 0, 1, 0)
- InternetCloseHandle(lngINetConn)
- InternetCloseHandle(lngINet)
- xlBook = xlApp.Workbooks.Open(sLocation)
- xlSheet = xlBook.Sheets(1)
- xlApp.Visible = True
- With xlSheet
- .Columns(1).TextoColums(
- Destination:= .Cells(6, 1),
- Datatype:=Excel.XlTextParsingType.xlDelimited,
- Textqualifier:=Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
- Semicolon:=True,
- DecimalSeparator:=".")
- End With
- End Sub
- Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (
- ByVal sAgent As String,
- ByVal lAccessType As Int32,
- ByVal sProxyName As String,
- ByVal sProxyBypass As String,
- ByVal lFlags As Integer) As Int32
- Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (
- ByVal hInternetSession As System.IntPtr, ByVal sServerName As String,
- ByVal nServerPort As Integer, ByVal sUsername As String,
- ByVal sPassword As String, ByVal lService As Int32,
- ByVal lFlags As Int32, ByVal lContext As System.IntPtr) As System.IntPtr
- Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (
- ByVal hFtpSession As IntPtr, ByVal RemoteFile As String,
- ByVal NewFile As String, ByVal fFailIfExists As Boolean,
- ByVal dwFlagsAndAttributes As Integer, ByVal Flags As Integer,
- ByVal Context As IntPtr) As Boolean
- Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As IntPtr) As Boolean
- 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“ ()