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
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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