Einzelnen Beitrag anzeigen

diver03

Registriert seit: 11. Dez 2007
24 Beiträge
 
Turbo Delphi für Win32
 
#1

Ist Excel schon gestartet? Wie Excel offen lassen?

  Alt 8. Dez 2008, 10:47
Hallo,

mit der folgenden Procedur (nur der für die Frage wichtige Abschnitt) lese ich Exceldaten in
mein Programm ein.
Ich habe den Quellcode aus einem Tutorial vom Delphi-Treff.
Es funktioniert auch ganz gut.

Was mich aber stört ist, dass Excel und somit auch alle Exceldateien, die vor meinem
Aufruf schon offen waren, geschlossen werden.
Wie kann ich das verhindern, bzw. wie kann ich abfragen, ob Excel schon läuft
und dann nur meine Datei zusätzlich öffnen und später wieder schließen.

Delphi-Quellcode:
procedure ExcelMaschDatLesen();
  var sValue,filename:String;
      OleX : OleVariant;
      Zelle: String;
begin
  Form_Hauptformular.OpenDialog1.InitialDir := DatenPfad;
  Form_Hauptformular.OpenDialog1.DefaultExt := 'xls';
  Form_Hauptformular.OpenDialog1.Filter := 'Messwerte (*.xls)|*.xls';
  Form_Hauptformular.OpenDialog1.Title := 'Einlesen Excel Messwertdaten ';
  Form_Hauptformular.OpenDialog1.Options := [ofFileMustExist, ofHideReadOnly, ofExtensionDifferent];
  repeat
  begin
    if Form_Hauptformular.OpenDialog1.Execute then
    begin
      if (ofExtensionDifferent in Form_Hauptformular.OpenDialog1.Options) then
      begin
        beep;
        if MessageDlg('Dateityp falsch.'+chr(13)+'Wählen Sie eine Datei vom Typ "xls" aus',
                       mtError,[mbAbort, mbRetry], 0) = mrAbort then
        begin
          exit;
        end
      end
    end
    else
    begin
      exit;
    end
  end;
  until not (ofExtensionDifferent in Form_Hauptformular.OpenDialog1.Options);
  flcid := GetUserDefaultLCID;
  filename:=Form_Hauptformular.OpenDialog1.FileName;
  with Form_Hauptformular do
  begin
    ExcelApplication1.Connect;
    ExcelApplication1.Visible[flcid]:=true;
    ExcelApplication1.UserControl:=true;
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Open(filename,emptyParam,
                             1,emptyParam,emptyParam,emptyParam,emptyParam,
                             emptyParam,emptyParam,emptyParam,emptyParam,emptyParam,
                             emptyParam,emptyParam,emptyParam,flcid));
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets.Item[1] as ExcelWorkSheet);

      Zelle := 'H2';
      sValue:=ExcelWorksheet1.Range[Zelle,Zelle].Value[OleX];
    MDaten.Form_MDaten.EditMaschinenNrBl.Text := sValue;

    ExcelApplication1.DisplayAlerts[flcid] := False;
    ExcelWorksheet1.Disconnect;
    ExcelApplication1.Workbooks.Close(flcid);
    ExcelApplication1.Quit;
    ExcelApplication1.Disconnect;
  end;
end;
Gruß
Jens
  Mit Zitat antworten Zitat