Excel Interop - startIndex darf nicht länger als die Länge der Zeichenfolge sein

  • VB.NET

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

    Excel Interop - startIndex darf nicht länger als die Länge der Zeichenfolge sein

    Hallo,

    ich hatte mal ein kleines Script für die Arbeit gemacht, mit dem ein paar Kollegen Ersatzteile aus einem Lager ausbuchen können. Das Programm lief bisher einwandfrei, bis ich die Spalte Bemerkungen hinzugefügt habe, seither erhalte ich die Meldung:
    System.ArgumentOutOfRangeException: "startIndex darf nicht länger als die Länge der Zeichenfolge sein.Parametername: startIndex"
    Spoiler anzeigen

    System.ArgumentOutOfRangeException
    HResult=0x80131502
    Nachricht = startIndex darf nicht länger als die Länge der Zeichenfolge sein.
    Parametername: startIndex
    Quelle = mscorlib
    Stapelüberwachung:
    bei System.String.Substring(Int32 startIndex, Int32 length)
    bei System.String.Substring(Int32 startIndex)
    bei Lagerentnahmen.Form1.Button1_Click(Object sender, EventArgs e)
    bei System.Windows.Forms.Control.OnClick(EventArgs e)
    bei System.Windows.Forms.Button.OnClick(EventArgs e)
    bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    bei System.Windows.Forms.Control.WndProc(Message& m)
    bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
    bei System.Windows.Forms.Button.WndProc(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    bei Lagerentnahmen.My.MyApplication.Main(String[] Args)

    Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
    [Externer Code]


    Ich dachte mir, dass ich die Änderung einfach rückgängig mache und dann passt es schon, jedoch tritt der Fehler nun auch so sporadisch auf. Nachvollziehbar ist der Fehler für mich momentan nicht, ich denke er kommt aus dem Excel Interop?

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2. Datum = Now.ToString("yyyy-MM-dd HH:mm")
    3. ListBox1.Items.Clear()
    4. If cmb_BME.Text = "" Or cmb_Entnehmer.Text = "" Or cmb_Maschine.Text = "" Or cmb_MatNr.Text = "" Then
    5. MessageBox.Show("Das Ersatzteil kann nicht gebucht werden, da die Eingaben unvollständig sind!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    6. Exit Sub
    7. End If
    8. Dim xlApp As New Excel.Application
    9. Dim xlWorkBook As Excel.Workbook
    10. Dim Tabelle As Excel.Worksheet
    11. Dim KW As String = DateToWeek(Now)
    12. Dim file As String = Form2.tb_path.Text & Form2.tb_filename.Text & "_" & KW & ".xlsx"
    13. If Not System.IO.File.Exists(file) Then
    14. xlWorkBook = xlApp.Workbooks.Add
    15. Tabelle = xlWorkBook.Sheets(1)
    16. xlApp.Visible = False
    17. 'Überschriften
    18. Tabelle.Cells(1, 1).Value = Buchung
    19. Tabelle.Cells(1, 2).Value = Materialnr
    20. Tabelle.Cells(1, 3).Value = Bezeichnung
    21. Tabelle.Cells(1, 4).Value = Anzahl
    22. Tabelle.Cells(1, 5).Value = BME
    23. Tabelle.Cells(1, 6).Value = Maschine
    24. Tabelle.Cells(1, 7).Value = Vorname
    25. Tabelle.Cells(1, 8).Value = DatumString
    26. Tabelle.Cells(1, 9).Value = Bemerkung
    27. xlWorkBook.SaveAs(file)
    28. xlWorkBook.Close()
    29. xlApp.Quit()
    30. End If
    31. xlApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    32. xlWorkBook = xlApp.Workbooks.Open(file)
    33. Tabelle = xlWorkBook.Sheets(1)
    34. xlApp.Visible = False
    35. Dim row = xlWorkBook.Sheets(1).Cells(xlWorkBook.Sheets(1).Rows.Count, 1).End(Excel.XlDirection.xlUp).Offset(1).Row
    36. 'Buchungsdaten eintragen
    37. Tabelle.Cells(row, 1).Value = " "
    38. Tabelle.Cells(row, 2).Value = Microsoft.VisualBasic.Left(cmb_MatNr.Text, 8)
    39. Tabelle.Cells(row, 3).Value = cmb_MatNr.Text.Substring(9)
    40. Tabelle.Cells(row, 4).Value = TextBox1.Text
    41. Tabelle.Cells(row, 5).Value = cmb_BME.Text
    42. Tabelle.Cells(row, 6).Value = cmb_Maschine.Text
    43. Tabelle.Cells(row, 7).Value = cmb_Entnehmer.Text
    44. Tabelle.Cells(row, 8).Value = Datum
    45. Tabelle.Cells(row, 9).Value = TextBox2.Text
    46. 'Formatierung
    47. Tabelle.Range("A1:I1").Font.Bold = True
    48. Tabelle.Columns.AutoFit()
    49. xlApp.DisplayAlerts = False
    50. xlWorkBook.Save()
    51. xlWorkBook.Close()
    52. xlApp.Quit()
    53. Kill_ExcelInstanz()
    54. ListBox1.Items.Add(TextBox1.Text & " | " & cmb_BME.Text & " | " & cmb_MatNr.Text & " | " & cmb_Maschine.Text & " | " & cmb_Entnehmer.Text & " | " & Datum)
    55. cmb_BME.Text = "ST"
    56. cmb_Entnehmer.Text = ""
    57. cmb_Maschine.Text = ""
    58. cmb_MatNr.Text = ""
    59. TextBox1.Text = ""
    60. TextBox2.Text = ""
    61. End Sub
    Die Fehlermeldung sieht mir garnet nach Interop aus, sondern tritt scheints schön ordentlich im Button_Click auf.
    Und da kann es nur eine Zeile sein - die mit Substring()
    Der Fehler spricht für sich - also stimmt was mit dem Text nicht, der der ist offsichtlich zu kurz - er muss mindestens - schau in deinen Code - 9 Zeichen lang sein.