Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word/COM Question for Freter

Posted on 1999-08-03
19
Medium Priority
?
198 Views
Last Modified: 2010-04-04
Ok Freter,

lemmi have it :-)

Rob
0
Comment
Question by:RBertora
[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
  • 9
  • 9
19 Comments
 
LVL 2

Expert Comment

by:freter
ID: 1392611
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392612
thats cool
Rob.
0
 
LVL 2

Expert Comment

by:freter
ID: 1392613
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Author Comment

by:RBertora
ID: 1392614
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392615
also this file : VBIDE_TLB

Rob
0
 
LVL 2

Expert Comment

by:freter
ID: 1392616
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392617
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
 
LVL 2

Expert Comment

by:freter
ID: 1392618
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392619
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
 
LVL 2

Expert Comment

by:freter
ID: 1392620
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392621
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
 
LVL 2

Expert Comment

by:freter
ID: 1392622
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392623
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
 
LVL 17

Expert Comment

by:inthe
ID: 1392624
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
 
LVL 2

Accepted Solution

by:
freter earned 800 total points
ID: 1392625
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
 
LVL 7

Author Comment

by:RBertora
ID: 1392626
Thanks very much for the help.

Freter you deserve the points.

(thanks Barry aswell)

Rob. :-)
0
 
LVL 2

Expert Comment

by:freter
ID: 1392627
thx for the A rating :-))
0
 
LVL 7

Author Comment

by:RBertora
ID: 1392628
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
 
LVL 2

Expert Comment

by:freter
ID: 1392629
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 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