Einzelnen Beitrag anzeigen

diver03

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

Re: Ist Excel schon gestartet? Wie Excel offen lassen?

  Alt 9. Dez 2008, 12:00
So, jetzt hab ichs.

Delphi-Quellcode:
procedure ExcelMaschDatLesen();
  var sValue,filename:String;
      OleX : OleVariant;
      Zelle: String;
      excel : OleVariant;
      ExcelLaeuft : Boolean;
begin

....

  flcid := GetUserDefaultLCID;
  filename:=Form_Hauptformular.OpenDialog1.FileName;
  with Form_Hauptformular do
  begin
//
// prüfen ob Excel bereits läuft
//
  try
     excel := GetActiveOleObject('Excel.Application');
     ExcelLaeuft := TRUE;
     ShowMessage('excel läuft');
  except
     excel := CreateOleObject('Excel.Application');
     ExcelLaeuft := FALSE;
     ShowMessage('excel wurde gestartet');
     ExcelApplication1.Connect; // Wenn Excel noch nicht läuft, dann starten

  end;
//
// Excel Datei öffnen
//
    ExcelApplication1.Visible[flcid]:= TRUE;
    ExcelApplication1.UserControl := FALSE;
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Open(filename,
                             emptyParam, //UpdateLinks: OleVariant
                             TRUE, //ReadOnly: OleVariant
                             emptyParam, //Format: OleVariant
                             emptyParam, //Password: OleVariant
                             emptyParam, //WriteResPassword: OleVariant
                             emptyParam, //IgnoreReadOnlyRecommended: OleVariant
                             emptyParam, //Orign: OleVariant
                             emptyParam, //Delimiter: OleVariant
                             emptyParam, //Editable: OleVariant
                             emptyParam, //Notify: OleVariant
                             emptyParam, //Converter: OleVariant
                             emptyParam, //AddToMru: OleVariant
                             emptyParam, //Local: OleVariant
                             emptyParam, //CorruptLoad: OleVariant
                             flcid));
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets.Item[1] as ExcelWorkSheet);
      Zelle := 'H1';
      sValue:=ExcelWorksheet1.Range[Zelle,Zelle].Value[OleX];
    MDaten.Form_MDaten.EditHandelsbezeichnungBl.Text := sValue;

.... // Werte einlesen

    ExcelApplication1.DisplayAlerts[flcid] := FALSE;
    ExcelWorksheet1.Disconnect;
    ExcelApplication1.ActiveWorkBook.Close(FALSE, emptyParam, emptyParam, flcid); // Workbook schließen
    if ExcelLaeuft = FALSE then
    begin
      ExcelApplication1.quit; // Wenn Excel gestartet wurde, dann schließen
      ExcelApplication1.Disconnect;
    end;
  end;
  Application.BringToFront; // eigenem Delphiprogramm wieder den Focus geben und nach vorne bringen
end;
Der Befehl Application.BringToFront; war das, was mir noch gefehlt hatte.

Vielen Dank für eure Hilfestellung.

Gruß
Jens
  Mit Zitat antworten Zitat