Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word Ole Question

Posted on 1998-09-09
22
Medium Priority
?
806 Views
Last Modified: 2010-04-06
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  
0
Comment
Question by:tam97
[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
22 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339167
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.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339168
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.
0
 

Author Comment

by:tam97
ID: 1339169
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:tam97
ID: 1339170
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339171
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339172
tam97,

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

Zif.
0
 

Author Comment

by:tam97
ID: 1339173
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339174
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...
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339175
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?
0
 

Author Comment

by:tam97
ID: 1339176
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
0
 
LVL 4

Expert Comment

by:d003303
ID: 1339177
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
0
 

Expert Comment

by:tddian
ID: 1339178
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339179
tam97, nope no problem
0
 

Author Comment

by:tam97
ID: 1339180
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
0
 

Author Comment

by:tam97
ID: 1339181
Zif

Thanks a lot mate, speak to you again.

John
0
 
LVL 2

Expert Comment

by:333
ID: 1339182
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339183
tam97, wasn't my proposal of part2 not good?
0
 

Author Comment

by:tam97
ID: 1339184
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
0
 

Author Comment

by:tam97
ID: 1339185
Hi 333

Thanks very much for your help.

will get back to you, ref: comment to Zif

Regards John
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339186
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.
0
 
LVL 2

Accepted Solution

by:
Thaddy earned 1200 total points
ID: 1339187
Q1
 Delphi 4 code to start word maximized:
procedure TForm1.BitBtn2Click(Sender: TObject);
var
  UnKnown:iUnKnown;
begin
  try
    //Check for running object,
    //If so, use it
    OleCheck(GetActiveObject(CLASS_Application_,nil,UnKnown));
    OleCheck(UnKnown.QueryInterface(Application_,iword));
    Showmessage('Word is already running, so we''ll use it.');
   except
    //else, create an instance
        Showmessage('Word is NOT running, so we''ll create an instance of it');
    iWord:=coApplication_.create();
   end;
   //set to visible
    iWord.visible:=true;
    iWord.WindowState:=wdWindowStateMaximize;
  end;


Q2:Checking if word has quit
Look at the checking code used for question 1 and reset the variablke to nil like so

procedure Application.ActiveFormchange //for example
var
  UnKnown:iUnKnown;
begin
  //a bit of pseudocoding,but i guess the use is obvious
  if yourform is YourformtochangeClass then
  try
    //Check for running object,
    //If so, use it.
    //Ole check will fail if word has disappeared...
    OleCheck(GetActiveObject(CLASS_Application_,nil,UnKnown));
    Olevariant(iWord).Quit;//The cast saves on parameters
  except
    Showmessage('Word already terminated');
    iWord:=nil
     .....Changing code goes here
  end;
end;

Q3: USE DELPHI'S CODE COMPLETION FEATURE!!!!!!
This is what I do all the time.
After you instantiate an instance of Words application Object
You can find all methods with parameters.

If you need one of the IUnknown/Variant Object methods
first record a VBA macro and translate the generated code to delphi.

0
 

Author Comment

by:tam97
ID: 1339188
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
0

Featured Post

Independent Software Vendors: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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