Verbindung zu ExcelApplication1 kappen ohne Excel zu schließen



  • Hallo Forum

    Ich hoffe man kann mir hier helfen, da ich schon das ganze Internet abgesucht habe und keine Lösung gefunden habe, auch hier nicht.

    Ich möchte Excel öffnen, Werte reinschreiben und dann die Steuerung wieder dem Anwender überlassen.
    Das hat mal super geklappt als ich Excel mit

    Excel = Variant::CreateObject("Excel.Application");

    geöffnet habe.
    Das habe ich gezwungenermaßen rausgeschmissen, nachdem ich ein ActiveX importiert habe. Wenn ich diese Komponente in meinem Projekt drin habe bekommen ich immer eine Zugriffsverletzung bei

    Excel.OlePropertyGet("ActiveWorkBook");

    Wenn jemand eine Idee hat warum das ein Problem ist, immer her damit.

    Jetzt bin ich umgestiegen auf TExcelApplication. Damit bekomme ich keine Zugriffsverletzung mehr.

    Ich wüsste gerne wie ich verhindern kann, dass meine Anwendung Excel schließen will.

    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    
        ExcelApplication1 = new TExcelApplication(Form1);
        ExcelWorkbook1 = new TExcelWorkbook(Form1);
        ExcelWorksheet1 = new TExcelWorksheet(Form1);
        ExcelApplication1->Workbooks->Open(WideString("C:\\temp.xlsx"),TNoParam(),
            TNoParam(),TNoParam(), TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(),TNoParam(), TNoParam(),TNoParam(),TNoParam(),0);
        ExcelApplication1->Visible[0]=true;
    
        ExcelWorkbook1->ConnectTo(ExcelApplication1->Workbooks->get_Item(1));
        ExcelWorksheet1->ConnectTo(ExcelWorkbook1->Worksheets->get_Item(1));
    
        ExcelWorksheet1->Cells->set_Item(1, 1, "test");
    
        ExcelWorksheet1->Disconnect ();
        ExcelWorkbook1->Close();
        ExcelWorkbook1->Disconnect();
        ExcelApplication1->Workbooks->Close();
        ExcelApplication1->Quit();
        ExcelApplication1->Disconnect ();
    
        delete ExcelWorksheet1;
        delete ExcelWorkbook1;
        delete ExcelApplication1;
    }
    

    Das funktioniert ja toll, nur wenn ich meine Applikation beende, fragt Excel ob man speichern will oder nicht.
    Wie trenne ich mich von Excel, ohne Close?

    Ciao

    Marco

    Ach ja:
    Borland Builder 5.0 Update Pack 1



  • Vielleicht solltest du nicht explizit die Arbeitsmappe und Excel schließen?

    ...
    ExcelWorkbook1->Close();
    ...
    ExcelApplication1->Quit()
    ...
    

    Da du meinst, es passiert beim Beenden deines Programms: Vielleicht hast du auch noch irgendwo ne Instanz von TExcelApplication, bei der AutoQuit an ist?



  • Vielleicht solltest du nicht explizit die Arbeitsmappe und Excel schließen?

    Gut, mein Beispielcode ist insofern Blödsinn, weil ich ihm ja sage er soll Excel schließen. Das versucht er ja auch mit der Frage ob er speichern soll.

    Das tut er aber auch wenn ich es ihm nicht sage, also:

    ...
        ExcelWorksheet1->Cells->set_Item(1, 1, "test");
    
        ExcelApplication1->AutoQuit = false;
    
        delete ExcelWorksheet1;
        delete ExcelWorkbook1;
        delete ExcelApplication1;
    }
    

    Auch wenn ich AutoQuit auf false setze, fragt Excel am Ende der Button2Click ob es speichern soll oder nicht.

    Vielleicht hast du auch noch irgendwo ne Instanz von TExcelApplication, bei der AutoQuit an ist?

    Das Programm ist inzwischen nur noch ein kleines Testprogramm. Da gibt es sonst nichts mehr.

    Ich weiß nicht wie ich die Verbindung zu Excel unterbrechen kann, ohne dass Excel geschlossen wird.


Anmelden zum Antworten