Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi OLE Excel (https://www.delphipraxis.net/76663-ole-excel.html)

hmbg 7. Sep 2006 18:57


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:
  // 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');
//Edit: AktenZeichen ist vom Typ String

Den Code habe ich so übernommen, weil ich mich noch nicht damit auskannte und auf meine Bedürfnisse umgestellt.

thx hmbg

mkinzler 7. Sep 2006 19:09

Re: OLE Excel
 
Delphi-Quellcode:
ExcelApp.Workbooks.Open('c:\'+FloatToStr(AktenZeichen)+'.xls');

hmbg 8. Sep 2006 06:51

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

mkinzler 8. Sep 2006 07:12

Re: OLE Excel
 
Was machst du alles zwischen dem 1. und dem 2. Aufruf?

hmbg 27. Sep 2006 19:13

Re: OLE Excel
 
Delphi-Quellcode:
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;
So, da ist bestimmt einiges überflüssig da dran, aber ich beschäftige mich ja auch noch nicht lange damit :-P

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

volkerw 27. Sep 2006 20:01

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:
ExcelApp.Quit;
sollte kommen:
Delphi-Quellcode:
ExcelApp := Unassigned;
Dazu ist
Delphi-Quellcode:
uses Variants;
nötig.
Gruß Volker

hmbg 28. Sep 2006 20:43

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 06:04 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz