?
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
Medium Priority
?
158 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 300 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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