Link to home
Start Free TrialLog in
Avatar of tam97
tam97

asked on

Word Ole Question

Can anyone help with this 3 part Ole question.

I'm looking for some tried and tested coding using the variant ('Word.Application') as against ('Word.Basic').

1/ I wish to have Word open maximized rather than the default normal. I was given the following code which does work great for ('Word.Basic')

     If OpenDialog1.Execute Then
     BEGIN
          WordApp:=CreateOleObject('Word.Basic');
          WordApp.AppMaximize;
          WordApp.Appshow;
          WordApp.FileOpen(OpenDialog1.filename);
     END;{if}
Any ideas for ('Word.Application')

2/ Quitting Word, whilst leaving one form going to another, for example, returning to a menu screen after using a working screen.
Using the following code

      IF NOT VarIsEmpty (WordApp) THEN
      BEGIN
           WordApp.Quit;
           MenuScreen.Show;
           WorkingScreen.Hide;
      END;{if}
seems to work ok if quitting Word using this procedure, but if you close Word from within itself, an error is generated, does anyone know coding to overcome this problem.

3/ Is there a simple way of finding Word's Ole object methods, for instance a list of methods, e.g Visible
                                     Documents
                                     Quit
                                     etc.
I've tried to find out by importing Word's Type Library and had a brain meltdown.

If anyone can help they will deserve the points offered.

Regards
John  
Avatar of ZifNab
ZifNab

Hi John,

0. A good article is this one :

http://members.aol.com/charliecal/ 

go to OLE automation: Delphi, Word and Excel.
http://hometown.aol.com/charliecal/ArticlesAndCode.html

-----> READ THIS ARTICLE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

--> very good explenation on how to get the commands you can use by OLE.


1. give me some time and I'll find it too.

2. Use exceptions :

If VarIsEmpty(WordApp) then begin
 try
  WordApp.Quit
 except {on EOleSysError  do}
  ...
 end;
 ...
end;

3. {from article}

You can determine all the constants used by Excel by reading its type library. There are at least two simple ways to read a type library.

You can read the type library with a third party tool, such as the OleView application that ships with the Microsoft SDK.
You can ask Delphi to read the library for you, and to translate the information stored in the library into Object Pascal. Obviously, this is the preferred technique.
I have included the translations of the Excel and Word type libraries with this article. However, if you want to create your own versions of these libraries, then you can select Project | Import Type Library from the Delphi menu, and then select the appropriate type library. A Delphi translation of the type library will be created automatically. (Be sure you are using Delphi 3.01 or later when importing the type library, as some important fixes were made to this technology between Delphi 3.0 and Delphi 3.01.)

There will be a number of warnings at the top of the type library, but you can ignore them. Most of these warnings have to do with name conflicts. For instance, if Excel uses an Object Pascal keyword in its code, then we need to make an alias for that keyword. For instance, Excel uses End in several places. This is a Delphi keyword, and so we append a space on to the end of it, converting the word End to End_. This puts an end to name conflicts.

The files you want to import usually have either a TLB or EXE extension. When working with Office 97, however, you want one with an OLB extension. The file to use with Word is MSWORD8.OLB, and the one to use with Excel is EXCEL8.OLB. On my system, I found these entries in the …\Microsoft Office\Office directory.

The Pascal translations of the interfaces to all the objects used in Excel or Word are found in the files created by importing EXCEL8.OLB and MSWORD8.OLB. Throughout this part of the paper, I will ignore these interfaces, and show you how to work directly with variant objects. However, in Part II of the paper I will return to this subject, and show you how to work with interfaces. At that time, I will present a discussion of the relative merits of working with variants and interfaces.


Regards, Zif.
tam97,

1. Isn't it just WordApp.Maximize (or something like this?) I don't have word on this computer, so I can't look in it's library. But follow the instructions on point 3 and you get everything you need!

Forgot something, it's better like this :

2. Use exceptions :

If VarIsEmpty(WordApp) then begin
 try
  WordApp.Quit
 except on EOleSysError  do
  WordApp := unassigned;
 end;
 ...
end;

Regards, Zif.
Avatar of tam97

ASKER

Hi Zif

good to hear from you again.

With regards to the code

If VarIsEmpty(WordApp) then begin
 try
  WordApp.Quit
 except {on EOleSysError  do}

 could you explain what you mean in the line
 except {on EOleSysError do} ?

I have the article you mentioned but was wondering if a document or other exsists that lists all or most of the methods applicable to the Word Ole object

Regards John
Avatar of tam97

ASKER

Zif

Just noticed your extra comment regarding EOleSysError, please ignore my question on that point.

Regarding WordApp.Maximize;
this generates the following error message, "EOleError method 'Maximize' not supported by automation object"

John
about tam97,

 if you specify
  on EOleSysError do begin
   {.... handle here for only OLESysErrors }
  end;              
 
if you use
  except
   {without on's......}
   {this code in here will be excuted by every sort of exception, not only OLE errors}
  end;

About Article :
 no I don't think this exist (at least I haven't seen such)
 Maybe some special books? At microsoft itself (MSDN)?

 I mostly figured it out by looking at Excel or at there demo's

--- from document :
For instance, Excel has extensive COM documentation in an online help file that ships with Microsoft Office called VBAXL8.HLP. (Break it down: VBA: Visual Basic for Applications, XL: Excel, 8: Version number.) If you are doing a lot of OLE Automation with Excel then you should add this file to Delphi’s tools menu so you can get at it easily. For information on retrieving objects, use the Index feature in the Excel help to look up "OLE programmatic identifiers".
-----------
PS. good url (got it myself from yeurk) :

http://sunsite.informatik.rwth-aachen.de/delphi/ftp/d30free/data2wrd.zip
tam97,

mmmmm so Maximize doesn't exist... damn, so then it has to be another one... Well, I'll keep looking...

Zif.
Avatar of tam97

ASKER

Zif

Hi again, it's obvious we're both on-line at the moment.

Have just returned from downloading OleView, will look at it shortly.

Will visit the site you gave.

By the way I've tried many different ways to maximize using Word.Application to no avail, I used the section 'Visual Basic Equivalents for WordBasic Commands'from the Vbawrd8 help file, still no luck.

Regards John
yes, we're online, but not anymore a long time for me.
mmm. strange I always thought that I imported some sort of library next to the constants, in which you could see all the objects... but I can't find it anymore in here...
mmm, are you sure you translated all the possible files? (point 2 of possibility), because I know I did it once and then I had two files... the constants and then another one with everything in it (it wasn't usefull to use, but I could see every function). I'm sure it was with importing it to delphi... Zif.

Sorry I could'nt help you much further... I'm sure that point 2 (importing with help of delphi) works... it was easy that time... So what goes wrong?
Avatar of tam97

ASKER

Zif

I hope your not offended if I re-open the question to other experts, I feel that we didn't quite make it on this one.

Thanks anyway for trying tonight and thanks for the help your've given in the past, I hope you won't be deterred from offering your assitance next time.

Best wishes
John
hmm, about the 3rd part, what did you import ? If you have Word 8, import .\Microsoft Office\Office\Msword8.olb
I presonally think it is very readable.

Slash/d003303
For "Maximize"...

Try out:

const
  wdWindowStateMaximized = 1;
var
  Word: Variant;

// Just in order to avoid invoking two Word apps
try
  Word := GetActiveOLEObject('Word.Application');
except
  Word := CreateOLEObject('Word.Application');
end;
// The actual line
Word.WindowState = wdWindowStateMaximized;

Cheers,
Christophe
tam97, nope no problem
Avatar of tam97

ASKER

Hi tddian

Your code for maximize worked perfectly, thanks a lot.

Could you offer any advice on part 2 of the question on quitting Word under the circumstances I quoted.

Ref: part 3 I get the message that I'll just have to learn how to read and understand the information stored in Word's type library.

If you could send your advice in as an answer to my question I can then award the points to your account.

Best regards
John
Avatar of tam97

ASKER

Zif

Thanks a lot mate, speak to you again.

John
tam97, wasn't my proposal of part2 not good?
Avatar of tam97

ASKER

Hi Zif

With regards to part 2, I could'nt get the code to work, the problem is I really am a beginner and as such is prone to make errors of judgement when replying back to the experts like yourself.

I'm sure I've done just that on this occasion, would you agree that I should award 100 points to each of the contributers who have provided help, e.g yourself for Part 2, tddian for Part1 and 333 for Part 3 ?.

And in future keep questions to one subject only.

Regards John
Avatar of tam97

ASKER

Hi 333

Thanks very much for your help.

will get back to you, ref: comment to Zif

Regards John
Hi tam97,

you don't have to split the points... If you really want to give me points, 50 is enough, but it isn't working yet, so don't give me points, yet. I'm sorry but I'm going on vacation in about 2 hours...

Regards, Zif.
ASKER CERTIFIED SOLUTION
Avatar of Thaddy
Thaddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tam97

ASKER

Hi Thaddy

Sorry for not getting back sooner, I was away for a few days.

Thanks for the code you supplied, it was a great help.

Please accept the points attached to question.

Rgds
John