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
150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE2 application frozen on Windows 10 10 278
find a node in VST 2 64
Open a URL with Internet Explorer in a new tab (not a new window) 1 74
How to debug For loops? 3 46
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

27 Experts available now in Live!

Get 1:1 Help Now