![]() |
OLE Excel
Hi
Ich verwende ein Excel OLE Objekt um Daten in eine Excel Tabelle zu schreiben. Dazu wird der entsprechende Prozess erzeugt, wenn er nicht vorhanden ist. Das Problem ist, wenn er vorhanden wird, kommt, bevor irgendwelche Daten ausgetauscht werden, der Fehler: "Variante des Typs(String) konnte nicht in Typ(Double) konvertiert werden." Und das bevor mein Programm eingreift, beim öffnen der Datei. Vielleicht kann mir ja jemand helfen Hier noch der fragliche Code:
Delphi-Quellcode:
//Edit: AktenZeichen ist vom Typ String
// By using GetActiveOleObject, you use an instance of Word that's already running,
// if there is one. try ExcelApp := GetActiveOleObject('Excel.Application'); except try // If no instance of Word is running, try to Create a new Excel Object ExcelApp := CreateOleObject('Excel.Application'); except ShowMessage('Excel - Modul kann nicht gestartet werden! Möglicherweise ist es nicht installiert?'); //Exit; end; end; // Open a Workbook, Arbeitsmappe öffnen ExcelApp.Workbooks.Open('c:\'+AktenZeichen+'.xls'); Den Code habe ich so übernommen, weil ich mich noch nicht damit auskannte und auf meine Bedürfnisse umgestellt. thx hmbg |
Re: OLE Excel
Delphi-Quellcode:
ExcelApp.Workbooks.Open('c:\'+FloatToStr(AktenZeichen)+'.xls');
|
Re: OLE Excel
Das erste mal öffnet er die Datei ja und schreibt auch hinein (AktenZeichen ist von Typ String)
Nur das zweite mal unter dem gleichen Prozess geht nicht mehr. mfg hmbg |
Re: OLE Excel
Was machst du alles zwischen dem 1. und dem 2. Aufruf?
|
Re: OLE Excel
Delphi-Quellcode:
So, da ist bestimmt einiges überflüssig da dran, aber ich beschäftige mich ja auch noch nicht lange damit :-P
procedure TForm1.ProceedButtonClick(Sender: TObject);
var i, RowCount, ORowCount:integer; begin i:=1; // By using GetActiveOleObject, you use an instance of Word that's already running, // if there is one. try ExcelApp := GetActiveOleObject('Excel.Application'); except try // If no instance of Word is running, try to Create a new Excel Object ExcelApp := CreateOleObject('Excel.Application'); except ShowMessage('Excel - Modul kann nicht gestartet werden! Möglicherweise ist es nicht installiert?'); //Exit; end; end; // Open a Workbook ExcelApp.Workbooks.Open('c:\'+AktenZeichen+'.xls'); repeat i:=i+1; until ExcelApp.Cells[i, 1].Value=''; ORowCount:=i; ExcelApp.Cells[ORowCount, 2].Value:=DateToStr(Datum.Date); ExcelApp.Cells[ORowCount, 3].Value:=StundenBeschreib.Lines.Text; RowCount:=ORowCount; for i:=0 to KostenUebers.Items.Count-1 do begin RowCount:=RowCount+1; ExcelApp.Cells[RowCount, 3].Value:=KostenUebers.Items.Strings[i]; ExcelApp.Cells[RowCount, 6].Value:=FloatToStr(EinzelKosten[i+1]); end; try for i:=0 to Data.Count-1 do begin RowCount:=RowCount+1; Data.Strings[i]:=AnsiMidStr(Data.Strings[i],0,Length(Data.Strings[i])-12); if MB = 'Sachverständiger' then begin ExcelApp.Cells[RowCount, 3].Value:=Data.Strings[i]; ExcelApp.Cells[RowCount, 4].Value:=' -'; ExcelApp.Cells[RowCount, 5].Value:=' -'; end; if MB = 'Hilfskraft' then begin ExcelApp.Cells[RowCount, 4].Value:=Data.Strings[i]; ExcelApp.Cells[RowCount, 3].Value:=' -'; ExcelApp.Cells[RowCount, 5].Value:=' -'; end; if MB = 'Schreibkraft' then begin ExcelApp.Cells[RowCount, 5].Value:=Data.Strings[i]; ExcelApp.Cells[RowCount, 4].Value:=' -'; ExcelApp.Cells[RowCount, 3].Value:=' -'; end; end; except end; if MB = 'Sachverständiger' then begin ExcelApp.Cells[ORowCount, 4].Value:=StundenEdit.Text; ExcelApp.Cells[ORowCount, 5].Value:=' -'; ExcelApp.Cells[ORowCount, 6].Value:=' -'; end; if MB = 'Hilfskraft' then begin ExcelApp.Cells[ORowCount, 5].Value:=StundenEdit.Text; ExcelApp.Cells[ORowCount, 4].Value:=' -'; ExcelApp.Cells[ORowCount, 6].Value:=' -'; end; if MB = 'Schreibkraft' then begin ExcelApp.Cells[ORowCount, 6].Value:=StundenEdit.Text; ExcelApp.Cells[ORowCount, 4].Value:=' -'; ExcelApp.Cells[ORowCount, 5].Value:=' -'; end; for i:=ORowCount to RowCount do ExcelApp.Cells[i, 1].Value:=inttostr(i); // Save the active Workbook: ExcelApp.ActiveWorkBook.Save;//As('c:\'+AktenZeichen+'.xls'); ExcelApp.ActiveWorkBook.Close; ShowMessage('File Saved'); ExcelApp.Quit; end; Das ist jetzt die ganze procedure, wo die Daten, die vorher eingegeben werden in die Tabelle geschrieben werden. Zuerst hat das Programm auch noch gut funktioniert, ich weiß aber nicht ab welchem Schritt die Probleme anfingen nya, mfg hmbg |
Re: OLE Excel
Hallo hmbg,
die Variant-Variablen müssen noch auf unassigned gesetzt werden, sonst bleibt Excel laufend unsichtbar im Hauptspeicher. Kurz, nach
Delphi-Quellcode:
sollte kommen:
ExcelApp.Quit;
Delphi-Quellcode:
Dazu ist
ExcelApp := Unassigned;
Delphi-Quellcode:
nötig.
uses Variants;
Gruß Volker |
Re: OLE Excel
In "SB.EXE" ist eine Exception vom Typ EVariantTypeCastError aufgetreten. Meldung: "Variante des Typs(String) konnte nicht in Typ(Double) konvertiert werden."
Leider tritt der Fehler immernoch auf, beim zweiten speichern :-( Anscheinend macht mein programm die Datei kaputt, denn man kann sie hinterher auch nicht mehr mit excel öffnen naja, ich hab kein plan, thx mfg hmbg |
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:20 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz