Board index » delphi » Creating Files (Word, Excel, etc) from Delphi

Creating Files (Word, Excel, etc) from Delphi

Hallo!

I have a little problem here. I try to create Word Excel or any other
registered Filetype from my Delphi Application. In short: It doesnt work.
Did anyone do this or has some sourcecode or anything which I can look at

Thanks in advance

Markus

 

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Markus Pfeiffer wrote in message ...
>Hallo!

>I have a little problem here. I try to create Word Excel or any other
>registered Filetype from my Delphi Application. In short: It doesnt work.
>Did anyone do this or has some sourcecode or anything which I can look at

Try to look at this question freshly, and then try to
answer it.

It would help if you told us _how_ you're trying to "create"
these files. If you are, for example, reading existing (and
valid) Word documents and writing them out verbatim, you are
simply not having this problem. So obviously you're trying it
some different way. How?

Groetjes,
Maarten Wiltink

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
> Try to look at this question freshly, and then try to
> answer it.

> It would help if you told us _how_ you're trying to "create"
> these files. If you are, for example, reading existing (and
> valid) Word documents and writing them out verbatim, you are
> simply not having this problem. So obviously you're trying it
> some different way. How?

I believe that he wants to do the same thing that right click
on the desktop can do

i.e. you can create any new file with appropriately registered application
by running this application

I have no I idea how to do that either.

regards
Majkel
--
www.zabezp.prv.pl - Zabezpieczanie Programw Komputerowych
www.wbm.prv.pl - Wirtualna Burza Mzgw o Unii Europejskiej

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Majkel wrote:
>> Try to look at this question freshly, and then try to
>> answer it.

>> It would help if you told us _how_ you're trying to "create"
>> these files. If you are, for example, reading existing (and
>> valid) Word documents and writing them out verbatim, you are
>> simply not having this problem. So obviously you're trying it
>> some different way. How?

> I believe that he wants to do the same thing that right click
> on the desktop can do

> i.e. you can create any new file with appropriately registered application
> by running this application

> I have no I idea how to do that either.

Well, perhaps I should have posted the question exactlty that way. I want to
create files like I can create them from the Desktop or any other folders
context menu. I have no Idea how to do that...

Cheers Markus

Re:Creating Files (Word, Excel, etc) from Delphi


Well, here is an example of a function that creates an Excel file and copies
the contents of a StringGrid to it.
In order to make it work, you have to add the units Grids and ComObj to the
"uses" line.

//--------------------------------------------
function StringgridToExcel(StringGrid: TStringGrid; FileName, SheetName:
string; SaveAsXcl, OpenExcel: boolean): boolean;
// add to "uses"-clause: grids and comobj
const xlWBATWorksheet = -4167; xlHAlignCenter=3; xlHAlignRight=4;
      xlMaximized=-4137; xlMinimized=-4140;
var Row,Col: integer;
    XLApp, Sheet: OLEVariant;
    ColumnRange: variant;
begin
  Result:=false;
  XLApp:=CreateOleObject('Excel.Application');
  try
    XLApp.Visible:= OpenExcel;
    XLApp.Workbooks.Add(xlWBatWorkSheet);
    Sheet:= XLApp.Workbooks[1].WorkSheets[1];
    Sheet.Name:='Johnson';   // or whatever...
    sheet.cells[1,1].font.size:=18;
    sheet.cells[1,1].font.bold:=true;
    sheet.cells[1,1].font.italic:=true;
    sheet.cells[1,1].font.color:=$880000;
    sheet.range['A1','B5'].HorizontalAlignment := xlHAlignRight;
    ColumnRange := XLApp.Workbooks[1].WorkSheets['Johnson'].Columns;
    ColumnRange.Columns[1].ColumnWidth := 45;
    sheet.range['B6','E39'].HorizontalAlignment := xlHAlignCenter;
    sheet.range['A7','E8'].font.bold:=true;
    sheet.range['A30','B30'].font.bold:=true;
    for col:=0 to StringGrid.ColCount-1 do for row:=0 to
StringGrid.RowCount-1 do
                   Sheet.Cells[row+1,col+1]:=StringGrid.Cells[col,row];
//Mind the +1 !!
    try
      if SaveAsXcl then XLApp.Workbooks[1].SaveAs(FileName);
      Result:=True;
    except
       // Error ?
    end;
  finally
    if not VarIsEmpty(XLApp) then begin
      XLApp.DisplayAlerts:=False; XLApp.Quit;
      XLAPP:=Unassigned; Sheet:=Unassigned;
    end;
  end;
end;
//-------------------------------------------------

Hope this is what you are looking for!

Niko Roorda

"Markus Pfeiffer" <profm...@profmakx.org> schreef in bericht
news:advo28$smr$1@nets3.rz.RWTH-Aachen.DE...

Quote
> Hallo!

> I have a little problem here. I try to create Word Excel or any other
> registered Filetype from my Delphi Application. In short: It doesnt work.
> Did anyone do this or has some sourcecode or anything which I can look at

> Thanks in advance

> Markus

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Niko wrote:
> Well, here is an example of a function that creates an Excel file and
> copies the contents of a StringGrid to it.
> In order to make it work, you have to add the units Grids and ComObj to
> the "uses" line.

Well what youve done is exactly what I try to prevent. Lets say the stupid
luser has installed Staroffice. Then all this doesnt work anymore. Further
I want to create Corel Files for example. The Registry does give me some
hints how to do this, but I just dont know how to do it...

Cheers

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Markus Pfeiffer wrote in message ...
>Majkel wrote:
[...]
>> I believe that he wants to do the same thing that right click
>> on the desktop can do
>Well, perhaps I should have posted the question exactlty that way. I want
to
>create files like I can create them from the Desktop or any other folders
>context menu. I have no Idea how to do that...

There is a ShellNew directory under the Windows directory for
Windows 9x and in the user profile for NT. I'm not sure merely
putting a file (of a registered type) is sufficient, though.

Putting a ShellNew key under the extension key in HKCR, with an
empty string value named NullFile in it, lets you create an empty
new file with that extension.

Groetjes,
Maarten Wiltink

Re:Creating Files (Word, Excel, etc) from Delphi


Ok, I see what you mean. Of course, there is always the possibility of
constructing the file all by yourself.
For Word, that's easy: you can use a TRichEdit to make and save an RTF file.
Of course, there are some limitations: e.g. you cannot add pictures in a
TRichEdit. There are improved versions like TRichEditEx, but you'll have to
buy that - so far, I didn't find a good extended TRichEdit that's for free.

Other kinds of files, like excel or Corel things, are not as easy available,
as far as I know. So, in that case you can look for the exact structure of
those file types: if you search for them with Google, using as a search
phrase "files formats", you will find plenty of sites that offer these
descriptions. Then, you can write a program that constructs your file, line
by line. It's a lot of work, but it can be done.

Niko

"Markus Pfeiffer" <profm...@profmakx.org> schreef in bericht
news:ae2oi8$huh$3@nets3.rz.RWTH-Aachen.DE...

Quote
> Niko wrote:

> > Well, here is an example of a function that creates an Excel file and
> > copies the contents of a StringGrid to it.
> > In order to make it work, you have to add the units Grids and ComObj to
> > the "uses" line.

> Well what youve done is exactly what I try to prevent. Lets say the
stupid
> luser has installed Staroffice. Then all this doesnt work anymore.
Further
> I want to create Corel Files for example. The Registry does give me some
> hints how to do this, but I just dont know how to do it...

> Cheers

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Niko wrote:
> Ok, I see what you mean. Of course, there is always the possibility of
> constructing the file all by yourself.
> For Word, that's easy: you can use a TRichEdit to make and save an RTF
> file. Of course, there are some limitations: e.g. you cannot add pictures
> in a TRichEdit. There are improved versions like TRichEditEx, but you'll
> have to buy that - so far, I didn't find a good extended TRichEdit that's
> for free.

> Other kinds of files, like excel or Corel things, are not as easy
> available, as far as I know. So, in that case you can look for the exact
> structure of those file types: if you search for them with Google, using
> as a search phrase "files formats", you will find plenty of sites that
> offer these descriptions. Then, you can write a program that constructs
> your file, line by line. It's a lot of work, but it can be done.

I think from what I saw in the REgistry that it provides everything I need.
I just try to find out how to use it. I font think that creating a file
structure on my own is productive. And it isnt as flexible as it has to be

Thanks for your help anyway.

Markus

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
> I have a little problem here. I try to create Word Excel or any other
> registered Filetype from my Delphi Application. In short: It doesnt work.
> Did anyone do this or has some sourcecode or anything which I can look at

To create Word (rtf) documents you may try EK RTF report components
for Delphi. Full functional version available for download from
http://ekrtf.code.net.ru

Report templates are rtf files visually designed in Word or any other
RTF editor.  A lot of examples at
http://ekrtf.code.net.ru/help/albumframe.html

--
Thanks,
Eugene.

Re:Creating Files (Word, Excel, etc) from Delphi


Did anyone know web sites like these, but for Excel Files ?

If someone knows more about Excel Files Format ... I have to make ones from
applications running on PC on which Excel is not installed.

Thanks,
Amelie.

Re:Creating Files (Word, Excel, etc) from Delphi


AmelieAppletown wrote

Quote
> Did anyone know web sites like these, but for Excel Files ?

> If someone knows more about Excel Files Format ... I have to make ones from
> applications running on PC on which Excel is not installed.

Create An Excel File Without OLE ?
http://www.swissdelphicenter.ch/en/showcode.php?id=725

Re:Creating Files (Word, Excel, etc) from Delphi


On 12 Jun 2002 19:38:22 GMT, amelieapplet...@aol.com (AmelieAppletown)
wrote:

Quote
>Did anyone know web sites like these, but for Excel Files ?

>If someone knows more about Excel Files Format ... I have to make ones from
>applications running on PC on which Excel is not installed.

Hi Amelie,
This one uses OLE:
function SaveAsExcelFile(StringGrid: TStringGrid; TheFile: string):
boolean;
const
  xlWBATWorksheet = -4167;
var
  Row, Col:     integer;
  GridPrevFile: string;
  XLApp, Sheet: OLEVariant;
begin
  Screen.Cursor := crHourGlass;
  Result := false;
  XLApp := CreateOleObject('Excel.Application');
  try
    XLApp.Visible := False;
    XLApp.Workbooks.Add(xlWBatWorkSheet);
    Sheet      := XLApp.Workbooks[1].WorkSheets[1];
    Sheet.Name := 'Sheet1';
    for col := 0 to StringGrid.ColCount - 1 do
      for row := 0 to StringGrid.RowCount - 1 do
        Sheet.Cells[row + 1,col + 1] := StringGrid.Cells[col, row];
    try
      XLApp.Workbooks[1].SaveAs(TheFile);//(Initdir+TheFile);
      Result := True;
    except
       // Error ?
    end;
  finally
    if not VarIsEmpty(XLApp) then
    begin
      XLApp.DisplayAlerts := False;
      XLApp.Quit;
      XLAPP := Unassigned;
      Sheet := Unassigned;
    end;
  end;
 Screen.Cursor := crDefault;
end;
There are ways to do it without OLE, but if you need it, I would
rather zip or cab it and send it to you. It is quite a bit longer.

Regards
Johan Smit

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Julian Eriksen wrote:
> AmelieAppletown wrote
>> Did anyone know web sites like these, but for Excel Files ?

>> If someone knows more about Excel Files Format ... I have to make ones
>> from applications running on PC on which Excel is not installed.

> Create An Excel File Without OLE ?
> http://www.swissdelphicenter.ch/en/showcode.php?id=725

I found out how Win does it with its "New" Context menu... its quite a lot
easier than that :).

As you install Office, Setup creates a directory "ShellNew" in the Windows
folder. Everytime one creates a new file, windows copies and renames the
File which is referenced to in the registry. Im now implementing a few
functions to do this for me. But Im still asking myself why there arent
any Lib or System funcs for that :)

thanks anyway

Cheers Markus

Re:Creating Files (Word, Excel, etc) from Delphi


Quote
Markus Pfeiffer wrote in message ...

[...]

Quote
>As you install Office, Setup creates a directory "ShellNew" in the Windows
>folder. Everytime one creates a new file, windows copies and renames the
>File which is referenced to in the registry. Im now implementing a few
>functions to do this for me. But Im still asking myself why there arent
>any Lib or System funcs for that :)

That's easily answered. You noticed that Microsoft has been
sued over the last few years? One of the things people took
offense at was that not everything that Windows can do is
exposed to the application-programmer-at-large. But the guys
who write Office can have it all, of course.

You may have stumbled onto something that Microsoft decided
to keep for themselves, so they can build expensive Windows
applications that are prettier/work better than yours.

Groetjes,
Maarten Wiltink

Other Threads