• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Word/COM Question for Freter

Ok Freter,

lemmi have it :-)

Rob
0
RBertora
Asked:
RBertora
  • 9
  • 9
1 Solution
 
freterCommented:
Ok, First the procedure for building the type library import library:
(everything applies to Delphi 3, but should work exactly the same in Delphi 4)
1) from the Delphi menu bar, open "Project -> Import Type Library"
The Word /Excel / ... typelibs are not registered there, so we'll have to look them up by ourselves:
2) click on "Add" and browse to the directory where all your Office exe files are located in (\program files\msoffice\office\). Look for *.OLB files. There should be around 10 .olb files, one of them being MSWORD8.OLB. Click "open", and voila, it's in the list of known typelibs.
3) Select a directory you would like Delphi to put the import library to (by default, this is (..\delphi 3\imports\), and click on "ok".

Delphi will now creat the import library and show it to you after a few seconds. Do not be afraid of all the Warnings at the top of this unit. They only mean that the typelibrary contained some words that are reserved Object Pascal keywords and have been modified slightly (i.e. from TYPE to TYPE_, in order to permit compiliation of this .PAS file).

Ok, that's for the typelib. I will post a second comment for the source code of the sample application. I will not post the source of the typelib imkport unit, since this is quite large (14770 lines on my machine).

<freter>
0
 
RBertoraAuthor Commented:
thats cool
Rob.
0
 
freterCommented:
Ok. This is the code for the sample application.
Here is what it does:

1) Start Word
2) Lets you open a document
3) scan entire document for bookmarks
4) save each bookmark (which embraces a portion of text) to disk into a doc file with the name of the bookmark.

Have fun with it!

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs,
  StdCtrls, Word_TLB;

type
  TForm1 = class(TForm)
    btnStartWord: TButton;
    btnExport: TButton;
    procedure btnStartWordClick(Sender: TObject);
    procedure btnExportClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  WordApp: _Application;

implementation

{$R *.DFM}

procedure TForm1.btnStartWordClick(Sender: TObject);
begin
  WordApp := CoApplication.Create;
  WordApp.Visible := true;
end;

procedure TForm1.btnExportClick(Sender: TObject);
var book: BookMark;
    doc, newdoc: _Document;
    i: integer;
    ovi,
    Template, NewTemplate, FileName,
    FileFormat, LockComments, PassWord,
    AddToRecentFiles, WritePassword, ReadOnlyRecommended,
    EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData,
    SaveASAOCELetter,  SaveChanges, OriginalFormat,
    RouteDocument: OleVariant;
    start, stop: integer;
begin


    start := gettickcount;

    // init
    Template := 'normal.dot';
    NewTemplate := false;
    FileFormat := wdFormatDocument;
    LockComments := false;
    PassWord := '';
    AddToRecentFiles := false;
    WritePassword := '';
    ReadOnlyRecommended := false;
    EmbedTrueTypeFonts := false;
    SaveNativePictureFormat := false;
    SaveFormsData := false;
    SaveASAOCELetter := false;
    SaveChanges := false;
    OriginalFormat := wdWordDocument;
    RouteDocument := false;

    WordApp.ScreenUpdating := false;
    WordApp.Visible := false;
    doc := WordApp.ActiveDocument;

    for i := 0 to doc.Bookmarks.Count -1 do begin
      ovi := i + 1;

      // copy section
      book := doc.bookmarks.Item(ovi);
      book.Range.Copy;

      // paste into new document
      newdoc := WordApp.Documents.Add (Template, NewTemplate);
      newdoc.Content.Paste;

      // save to disk
      FileName := 'c:\temp\freter\' + book.name;
      newdoc.SaveAs (FileName, FileFormat, LockComments, PassWord, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveASAOCELetter);

      // cose document window
      newdoc.Close(SaveChanges, OriginalFormat, RouteDocument);
    end;

    WordApp.ScreenUpdating := true;
    WordApp.Visible := true;

    stop := gettickcount;

    ShowMessage( floattostr( (stop-start) / 1000) );

end;

end.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

If you are satisfied with the code and everything, let me know, so I can post an answer. I you still have some questions, just ask, I will assist you. Need other demo code? Ask as well. I'm in the process of building a large database-driven app with Word / Automation and a 4GL language (no, it's not Delphi, but Forte, and I strongly dislike it), so I should have some other intersting samples. Just let me know what exactly you want to do.

<freter>
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
RBertoraAuthor Commented:
Ok, the word_tlb file has a uses clause that
includes Office_TLB

I can't find a file office.olb any where on the system..

???
rob
0
 
RBertoraAuthor Commented:
also this file : VBIDE_TLB

Rob
0
 
freterCommented:
whoops? on my machine, delphi put all of them into the same directory. strange!

ah! now i see your problem ;-) you are talking about office.olb...

now, the two file you mention do not have to be imported explicitly, since delphi did that job for you already. isn't this compiler surprising each time?

you can just go on with testing the sample code.

<freter>
0
 
RBertoraAuthor Commented:
My problem is I don't have office.olb anywhere on my machine, nor do I have vbide.olb.. ? but I do have several ms ones, like word and excell ones...

can you email me those two files?

Rob
0
 
freterCommented:
rob: i could email you the files, but i fear that this wouldn't help you out. have installed the entire office suite? if not, try installing at least all the VBA stuff.

anyway, post your email adress and i will send you the .olb files together with the import libraries.

<freter>
0
 
RBertoraAuthor Commented:
this is the address:

Robert.Bertora@maindec.co.uk

If it doesn't work then I will probably just have to install the whole office suit...
Rob



0
 
freterCommented:
Rob, did you get the files?
As I stated in the eMail, there are no Office.olb /VBIDE.olb files on my system as well, which is why I expect the solution to work on your system as well as on mine. Just give it a try.

Do you get "File not found" errors?

<freter>
0
 
RBertoraAuthor Commented:
Getting there, (got the open word to work)

can you give me a procedure to open a new docment, populate
it with some text, just say 5 lines, and do some formatting
like bold etc... centre align font change etc..

Rob.
0
 
freterCommented:
rob, try this:

procedure TForm1.btnStartWordClick(Sender: TObject);
var
  newdoc: _Document;
  Template, NewTemplate: OleVariant;
  fnt: _Font;
  rng: Range;
begin
  WordApp := CoApplication.Create;
  WordApp.Visible := true;

  Template := 'normal.dot';
  NewTemplate := false;
  newdoc := WordApp.Documents.Add (Template, NewTemplate);
  newdoc.Content.InsertAfter('This is a test. Line 1' + #13);
  newdoc.Content.InsertAfter('This is a test. Line 2' + #13);
  newdoc.Content.InsertAfter('This is a test. Line 3' + #13);
  newdoc.Content.InsertAfter('This is a test. Line 4' + #13);
  newdoc.Content.InsertAfter('This is a test. Line 5' + #13);

  fnt := CoFont.Create;
  fnt.Name := 'Arial';
  newdoc.Content.Font := fnt;

  fnt.Bold := 1;
  rng := newdoc.Paragraphs.Item(1).Range;
  rng.Font := fnt;

  fnt.AllCaps := 1;
  rng := newdoc.Paragraphs.Item(2).Range;
  rng.Font := fnt;

  fnt.Italic := 1;
  fnt.Bold := 1;
  rng := newdoc.Paragraphs.Item(3).Range;;
  rng.Font := fnt;

end;

<freter>
0
 
RBertoraAuthor Commented:
Thanks Freter, it is a great start
I don't have the time to mess with it now,
but I got all your code to work...
cool...

answer for your poitns.

Rob :-)
0
 
intheCommented:
Rob,
i have sent a file to you as well.
i found it one day and seems a ok tutorial type thing for word/excel with delphi.
Regards Barry
0
 
freterCommented:
Rob,
if you any further questions, do not hesitate to ask me. Please use freter@gmx.net when contacting me by mail.

May the source be with you!

<freter>
0
 
RBertoraAuthor Commented:
Thanks very much for the help.

Freter you deserve the points.

(thanks Barry aswell)

Rob. :-)
0
 
freterCommented:
thx for the A rating :-))
0
 
RBertoraAuthor Commented:
Cool, I spent some time mucking about and it looks really powerful, not to mention the promiss of it being quick has been kept.

Rob.
0
 
freterCommented:
nice to hear that, rob.
IMO, the only disadvantage is the fact you have to provide every stupid little parameter, even if you do not care about it. can get pretty annoying when setting the options...

nevertheless, have fun with that baby!

<freter>
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.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now