Mit TextBox verschiedene Events erkennen (1. Mit Activate Event bestehender Inhalt in Excel schreiben, 2. Mit .TextChanged Event neuer Wert in eine andere Excel Zeile schreiben)

  • VB.NET

Es gibt 16 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Mit TextBox verschiedene Events erkennen (1. Mit Activate Event bestehender Inhalt in Excel schreiben, 2. Mit .TextChanged Event neuer Wert in eine andere Excel Zeile schreiben)

    Liebe VB-Paradise Users,

    Ich hab mal wieder eine kleines Problem.
    Ich möchte von einer TextBox verschiedene Events auswerten. Wenn ich jeweils nur einen Event auswerte funktioniert der Code einwandfrei
    Ich möchte wenn die Textbox angeklickt wird (Me.Activated) den bestehenden Wert im Excel speichern. (Funktioniert auch)
    Nun möchte ich wenn der Wert geädert wird dies in einer neuen Zeile im Excel eintragen. Mit diesem Event will es aber nicht klappen.


    Was ist an
    If tb_value1.TextChanged Then
    falsch?

    Vielen Dank für Eure Hilfe

    VB.NET-Quellcode

    1. Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Activated
    2. oExcel = CreateObject("Excel.Application")
    3. oExcel.Visible = False
    4. oBook = oExcel.Workbooks.add
    5. oSheet = oBook.Worksheets(1)
    6. oSheet.Activate()
    7. 'Start a new workbook in Excel
    8. 'Set oExcel = CreateObject("Excel.Application")
    9. 'Set oBook = oExcel.Workbooks.Add
    10. 'Add data to cells of the first worksheet in the new workbook
    11. 'Set oSheet = oBook.Worksheets(1)
    12. oSheet.Range("A1").Value = "Wert vorher"
    13. oSheet.Range("B1").Value = "Wert nacher"
    14. oSheet.Range("A1:B1").Font.Bold = True
    15. oSheet.Range("A2").Value = tb_value1.Text
    16. 'oSheet.Range("B2").Value = "John"
    17. 'Save the Workbook and Quit Excel
    18. oExcel.DisplayAlerts = False
    19. oBook.SaveAs("C:\Documents and Settings\xxx\Desktop\TEST Programme")
    20. oExcel.DisplayAlerts = True
    21. oExcel.Quit()
    22. If tb_value1.TextChanged Then
    23. oExcel = CreateObject("Excel.Application")
    24. oExcel.Visible = False
    25. oBook = oExcel.Workbooks.add
    26. oSheet = oBook.Worksheets(1)
    27. oSheet.Activate()
    28. 'Start a new workbook in Excel
    29. 'Set oExcel = CreateObject("Excel.Application")
    30. 'Set oBook = oExcel.Workbooks.Add
    31. 'Add data to cells of the first worksheet in the new workbook
    32. 'Set oSheet = oBook.Worksheets(1)
    33. oSheet.Range("A1").Value = "Wert vorher"
    34. oSheet.Range("B1").Value = "Wert nacher"
    35. oSheet.Range("A1:B1").Font.Bold = True
    36. 'oSheet.Range("A2").Value = tb_value1.Text
    37. oSheet.Range("B2").Value = tb_value1.Text
    38. 'My.Settings.Save()
    39. 'Save the Workbook and Quit Excel
    40. oExcel.DisplayAlerts = False
    41. oBook.SaveAs("C:\Documents and Settings\xxx\Desktop\TEST Programme")
    42. oExcel.DisplayAlerts = True
    43. oExcel.Quit()
    44. End If
    45. End Sub
    @carepicha:: Zunächst solltest Du das excel-Objekt in die Klasse verschieben und seine Instanzierung aus dieser Prozedur in die Form_Load oder so übertragen, sonst wirst Du ggf. Probleme mit den vielen Instanzen bekommen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Danke für den Hinweis. Es konnte so gar nicht funktionieren weil es immer ein neues Excel erstellt hat und das alte mit den alten Werten überschrieben hatte.......

    Nun bekomme ich aber beim .Activate Befehl einen Fehler.
    ({"Exception from HRESULT: 0x800A01A8"})
    => Was ist das?
    Bin leider (noch) nicht so so geübt mit VB.
    Hat jemand eine Idee was ich falsch mache?

    Schon mal vorab Danke für Eure Unterstützung

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim oExcel As Object
    3. Dim oBook As Object
    4. Dim oSheet As Object
    5. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    6. 'Start a new workbook in Excel
    7. oExcel = CreateObject("Excel.Application")
    8. oExcel.Visible = False
    9. oBook = oExcel.Workbooks.add
    10. oSheet = oBook.Worksheets(1)
    11. End Sub
    12. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13. ' open the excel file
    14. oSheet.Activate()
    15. 'Add data to cells of the first worksheet in the new workbook
    16. 'Set oSheet = oBook.Worksheets(1)
    17. oSheet.Range("A1").Value = "Wert vorher"
    18. oSheet.Range("B1").Value = "Wert nacher"
    19. oSheet.Range("A1:B1").Font.Bold = True
    20. oSheet.Range("A2").Value = tb_value1.Text
    21. 'oSheet.Range("B2").Value = "John"
    22. 'Save the Workbook and Quit Excel
    23. oExcel.DisplayAlerts = False
    24. oBook.SaveAs("C:\Documents and Settings\xxx\Desktop\TEST Programme")
    25. oExcel.DisplayAlerts = True
    26. oExcel.Quit()
    27. End Sub

    carepicha schrieb:

    Exception from HRESULT: 0x800A01A8
    Tipp das mal bei Frau Google ein: hresult 0x800a01a8 excel. Sieht wie eine NullPointer-Exception aus, da musst Du noch iwas per New oder Create erstellen.
    Setze einen Haltepunkt auf die betreffende Zeile und sieh Dir den Wert der einzelnen Objekte an.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    SpaceyX schrieb:

    OPTION STRICT ON
    geht bei diesem Excel-Scheiß nicht. :S
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @SpaceyX:: Hast recht, ich hab da so ein altes Beispiel, das ging nur Off, aber dies hier sollte On gehen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    OK, Option Strict On ist nun an.

    Danke für den Link. Leider habe ich keine Kenntnisse C#
    Habe es trotzdem versucht.

    Die Application wird erstellt.
    Doch nun habe ich für das Workbook und Worksheet den Fehler;
    Option Strict On disallows late binding.

    Leider verstehe ich nicht warum. Ich wäre Euch dankbar wenn ihr mir auf die Sprünge helfen könnten.


    Vielen Dank


    VB.NET-Quellcode

    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2. 'Start a new workbook in Excel
    3. Dim xlsApplicationLogger As New Microsoft.Office.Interop.Excel.Application
    4. Dim xlsWorkBookLogger As New Microsoft.Office.Interop.Excel.Workbook
    5. Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    6. xlsApplicationLogger = CType(CreateObject("Excel.Application"), _
    7. Microsoft.Office.Interop.Excel.Application)
    8. xlsWorkBookLogger = CType(xlsWorkBookLogger.Workbooks.Add, _
    9. Microsoft.Office.Interop.Excel.Workbook)
    10. xlsWorkSheet = CType(xlsWorkSheet.Worksheets(1), _
    11. Microsoft.Office.Interop.Excel.Worksheet)
    12. xlsApplicationLogger.Visible = True
    13. End Sub

    carepicha schrieb:

    disallows late binding.
    Du hast die Variable als Object deklariert und weist ihr eine Excel-spezifische Instanz zu.
    Wenn Du Excel-spezifische Prozeduren aufrufst, sind die ja nicht Member von Object, sondern von Excel-iwas.
    Und das kann der Compiler mit Strict On nicht auflösen.
    Deklariere die Excel-Dinger korrekt, und wenn das nicht geht, mach Strict Off.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    carepicha schrieb:

    Dim xlsApplicationLogger As New Microsoft.Office.Interop.Excel.Application
    ...
    xlsApplicationLogger = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)

    Du erzeugst die Instanz bereits mit New.
    Deshalb ist das CreateObject nicht mehr nötig.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    @petaod

    Also dieser Teil läuft mit Option Strict On.

    VB.NET-Quellcode

    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2. 'Start a new workbook in Excel
    3. Dim xlsApplicationLogger As New Microsoft.Office.Interop.Excel.Application
    4. xlsApplicationLogger = CType(CreateObject("Excel.Application"), _
    5. Microsoft.Office.Interop.Excel.Application)
    6. End Sub


    Ist es möglich ein Excel mit Option StrictOn zu öffnen.
    Hat das jemand schon gemacht?
    Ich kriege es einfach nicht hin:-(

    Danke für Eure Unterstützung

    carepicha schrieb:

    Ist es möglich ein Excel mit Option StrictOn zu öffnen.
    Ja.
    Form, Button, leeres DataGridView und eine Excel-Tabelle neben der SLN-Datei:
    Excel mit Strict On

    VB.NET-Quellcode

    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2. Dim path As String = Application.StartupPath
    3. If (path.Contains("\bin\Debug") OrElse path.Contains("\bin\Release")) Then
    4. For i As Integer = 0 To 2
    5. Dim Index As Integer = path.LastIndexOf(""c)
    6. If (Index > 0) Then
    7. path = path.Substring(0, Index)
    8. End If
    9. Next
    10. End If
    11. path &= "\testfile2.xls"
    12. Try
    13. Dim MyConnection As System.Data.OleDb.OleDbConnection
    14. Dim DtSet As System.Data.DataSet
    15. Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
    16. MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & _
    17. "data source=" & path & "; " & _
    18. "Extended Properties=Excel 8.0;")
    19. MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Tabelle1$]", MyConnection)
    20. MyCommand.TableMappings.Add("Table", "TestTable")
    21. DtSet = New System.Data.DataSet
    22. MyCommand.Fill(DtSet)
    23. DataGridView1.DataSource = DtSet.Tables(0)
    24. MyConnection.Close()
    25. Catch ex As Exception
    26. MessageBox.Show(ex.ToString)
    27. End Try
    28. End Sub
    testfile2.xls
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    carepicha schrieb:

    Dim xlsApplicationLogger As New Microsoft.Office.Interop.Excel.Application
    xlsApplicationLogger = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
    Nochmals: Es ist absoluter Blödsinn, das Objekt erst mit New zu erzeugen und danach mit CreateObjekt nochmals zu erzeugen.
    Lass die zweite Zeile einfach weg.


    RodFromGermany schrieb:

    MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=" & path & "; Extended Properties=Excel 8.0;")
    Das ist natürlich ein komplett anderer Ansatz, der ein einzelnes Excel-Sheet quasi als ReadOnly-Datenbank anspricht.
    Wenn ReadOnly reicht, ist das u.U sogar die schnellere Methode.
    ABER: Den Microsoft-Jet-Treiber gibt es bei ab Win7/64 nicht mehr.
    Bitte den Microsoft.ACE.OLEDB-Provider verwenden.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --

    petaod schrieb:

    ABER:
    ich habe dieses Beispiel hier unter W7-64 getestet, allerdings habe ich hier ein Office 2003 drauf.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Je nach Kombination kann das noch funktionieren.
    Ganz genau kann man die komplette Story hier nachlesen.
    Auszug:
    There is no 64-bit version of the Jet Database Engine, the Jet OLEDB Driver, the Jet ODBC Drivers, or Jet DAO available.
    This is also documented in KB article 957570.
    On 64-bit versions of Windows, 32-bit Jet runs under the Windows WOW64 subsystem.
    ...
    Native 64-bit applications cannot communicate with the 32-bit Jet drivers running in WOW64.
    ...
    you should plan to migrate from Jet to the 2007 Office System Driver ... which allows you to read from and write to pre-existing files in either Office 2003 (.mdb and .xls) or the Office 2007 (*.accdb, *.xlsm, *.xlsx and *.xlsb) file formats.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    @petaod:: Ich seh gerade, das war eine 32-Bit-Anwendung.
    Alles klar. :D
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!