Andrés Botero
Andrés Botero

Reputation: 1040

Embed Excel file in Delphi 5

I'm trying embed an Excel file into my Delphi 5 application, so I can avoid my users just deleting the file accidentally.

Using the embedded file, I create it on disk with a Save dialog, and then open it with the Excel := CreateOleObject('Excel.Application'); method. I've seen examples on how to load a resource, using THandles, but I don't seem to get it working with Excel.WorkBooks.Open(EmbeddedExcelFile);.

Have you had to do something like this before? How would you do it?

Thanks!

Upvotes: 0

Views: 2181

Answers (2)

PA.
PA.

Reputation: 29339

You have to include the file as a resource. Say you have a blah.xls

Create a blah.rc file with the following content

blah RCDATA blah.xls

compile it with the resource compiler into blah.res

embed the RES file within the executable

{$R 'blah.res'}

in your application code, extract the file and run it with this code

procedure ExtractAndRun(resourceID:string; resourceFn:string);
 var
  resourceStream: TResourceStream;
  fileStream: TFileStream;
  tempPath: string;
  fullFileName: string;

 begin
  tempPath:=GetTempDir;
  FullFilename:=TempPath+'\'+resourceFN;
  if not FileExists(FullFilename) then
   begin
     resourceStream := TResourceStream.Create(hInstance, resourceID, RT_RCDATA);
     try
      fileStream := TFileStream.Create(FullFilename, fmCreate);
      try
       fileStream.CopyFrom(resourceStream, 0);
      finally
       fileStream.Free;
      end;
     finally
      resourceStream.Free;
     end;
   end;
  ShellExecute(0,'open', pchar(FullFilename), nil, nil, SW_SHOWNORMAL);
end;

you'll have to add ShellApi in your uses clause

maybe you'll need this GetTempDir function

function GetTempDir: string;
 var
  Buffer: array[0..MAX_PATH] of char;
 begin
  GetTempPath(SizeOf(Buffer) - 1, Buffer);
  result := StrPas(Buffer);
 end; 

invoke the function like this

 extractAndRun('blah','blah.xls');

Upvotes: 5

Eduardo Mauro
Eduardo Mauro

Reputation: 1515

I am pretty sure it will not work. You have to save the file in a temp folder, alter it and and then do whatever you want.

Upvotes: 1

Related Questions