Solved

How can I get feedback from excel to my application as to whether a document has been saved or not.

Posted on 2004-04-24
4
152 Views
Last Modified: 2010-04-05
My application opens an excel template and populate cells with data, but now Im interested as to whether or not I can get feedback from excel to show the document created has been saved. What might I be able to do to achieve this?

procedure TMyForm.CreateExcel;
var
  oXL, oWB, oSheet: Variant;
begin
  // Start Excel and get Application Object
  oXL := CreateOleObject('Excel.Application');
  oXL.Visible := True;

  //Get template workbook
  oWB := oXL.WorkBooks.Open(ExtractFilePath(Application.exename) + Template.xlt');
  oSheet := oWB.ActiveSheet;

  //Populate fields from database
  with Datamodule, IBQuerydo
  begin
    <Populate cells>
  end;

  //of Microsoft Excel's lifetime
  oXL.Visible := True;
  oXL.UserControl := True;
end;
0
Comment
Question by:mikelittlewood
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10907382

   monitor the file modified date...
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10907422
function FileLastModified(const TheFile: string): string;
var
  FileH : THandle;
  LocalFT : TFileTime;
  DosFT : DWORD;
  LastAccessedTime : TDateTime;
  FindData : TWin32FindData;
begin
  Result := '';
  FileH := FindFirstFile(PChar(TheFile), FindData) ;
  if FileH <> INVALID_HANDLE_VALUE then begin
   Windows.FindClose(FileH) ;
   if (FindData.dwFileAttributes AND
       FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
     FileTimeToLocalFileTime
      (FindData.ftLastWriteTime,LocalFT) ;
     FileTimeToDosDateTime
      (LocalFT,LongRec(DosFT).Hi,LongRec(DosFT).Lo) ;
     LastAccessedTime := FileDateToDateTime(DosFT) ;
     Result := DateTimeToStr(LastAccessedTime) ;
    end;
  end;
end;
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 10907818
The problem is that there is no file to monitor.
I use a template to create the new excel document, which automatically creates another new file with a new name ready to be saved (i.e. you dont re-save the template). This allows me to re-use the template over and over for new documents.
0
 
LVL 6

Accepted Solution

by:
Amir Azhdari earned 100 total points
ID: 10908195
place a stringgrid (10 X 10) and 2 buttons in the form and try this code :
button1 will put the data and button2 get the data and save that to the grid


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,comobj, OleServer, ExcelXP, Grids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    StringGrid1: TStringGrid;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}


var
Obj: OleVariant;

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
const
xlWBatWorkSheet = -4167;
var
WorkBook: variant;
V: Variant;
begin
V:= CreateOleObject('Excel.Application');
WorkBook := V.Workbooks.Add(xlWBatWorkSheet);
WorkBook.WorkSheets[1].Name := SheetName;
V.Visible := Show;
Result:= V;
end;


procedure ExcelPut(ExcelObject: Variant; SheetName: string;
Row, Col: Integer; Data: string; FontName: string = '';
FontSize: Integer = 0; Color: Integer = clBlack; FontStyles: TFontStyles = []);
var
Sheet: variant;
begin
Sheet:= ExcelObject.WorkBooks[1].WorkSheets[SheetName];
Sheet.Cells[Row, Col].Value:= Data;
Sheet.Cells[Row, Col].Font.Color:= Color;
if FontName <> '' then
  Sheet.Cells[Row, Col].Font.Name:= FontName;
if FontSize <> 0 then
  Sheet.Cells[Row, Col].Font.Size:= FontSize;
if FontSize <> 0 then
  Sheet.Cells[Row, Col].Font.Size:= FontSize;
Sheet.Cells[Row, Col].Font.Bold:= fsBold in FontStyles;
Sheet.Cells[Row, Col].Font.Underline:= fsUnderline in FontStyles;
Sheet.Cells[Row, Col].Font.Italic:= fsItalic in FontStyles;
end;


function excelget(excelobject:variant;sheetname:string;row,col:integer):string;
var sheet:variant;
begin
Sheet:= ExcelObject.WorkBooks[1].WorkSheets[SheetName];
result:=Sheet.Cells[Row, Col].Value;

end;

procedure ExcelClose(ExcelObject: Variant);
begin
ExcelObject.Quit;
end;


procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
begin
 Obj:= ExcelCreate('Test', True);
for i:=1 to 10 do
for j:=1 to 10 do
 ExcelPut(Obj, 'Test', i, j, 'Hello'+inttostr(i)+'X'+inttostr(j), 'Tahoma');
end;



procedure TForm1.Button2Click(Sender: TObject);
var i,j:integer;
begin
for i:=1 to 10 do
for j:=1 to 10 do
stringgrid1.Cells[j-1,i-1]:= Excelget(Obj, 'Test', i, j);


end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
excelclose(obj);
end;

end.

Regards
Azhdari
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question