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
149 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
Comment Utility

   monitor the file modified date...
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now