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

Open and Manage Multiple Word Docs


I would like to be able to open and manage multiple word docs from my Delphi app (myApp) in the following manner

a) assume myApp knows the names of the docs, and displays them eg in a listview

b) when the user clicks on a doc, it gets opened in Word (and I would like it not to be opened if it is already opened ie to avoid the message "do you want to revert to the saved document")

c) the newly opened word instance (window) gets positioned at a place and of a size under the control of myApp .. effectively I want a "cascading" appearance

d) the user can close any of the docs opened from myApp from myApp

e) if the user closes myApp, and there are Word instances running which were opened by myApp, then they get closed.

ShellExecute could be used to open the docs, but the returned handle won't help. Somehow I will need to find a process ID (or something?)  for the most recently opened instance so that can get stored in myApp. And for the window positioning and sizing, I need to do what?

I thought about using TOleContainer and had a look at the two delphi demos ..  MDI means that all the documents are contained within the MyApp mainform and share a single menu (where are the Word toolbars?), and this is not the sort of user experience I want.  Basically I want myApp to act as the "controller" or "launch pad" for a number of docs .. with something more than just a dumb launch. (ie resizing, positioning, only launching if not open etc etc)

Ideas, suggestions, and code would be welcome.

1 Solution
I would still use the TOleContainer, but you don't have to use MDI.  Look into the SDI demo.

Create a new application with a main form.  Then create a menu item to open the word doc.  Create a sub form to hold the TOleContainer and pass the file to open to the form.  When the user selects the menu item to open then create a new subform and use the show method instead of the showmodal.  Allow the new form to handle all aspects of the word doc to be opened.

The Application object has a list of forms in it that you can search through to see if your doc is already open.  The only drawback would be that if the user opened the doc outside of you app then you wouldn't know about it.  

If you want to look through all docs opened in windows then use the winapi callback function EnumWindows

Mutley2003Author Commented:
kfoster, thaks for the suggestions. I will give it a try.

this is not a solution but a key:

A unit called SndKey32.pas included on the Delphi installation CD (\Info\Extras\SndKey32.pas), makes it easy to simulate keyboard usage. This unit includes two routines that simulate popular VB routines: Sendkeys and AppActivate. SendKeys takes a PChar as its first parameter and a boolean as its second, like so:
SendKeys('KeyString', Wait) ;
where KeyString is a string of key names and modifiers that you want to send to the current input focus and Wait is a boolean variable or value that indicates whether SendKeys should wait for each key message to be processed before proceeding.
AppActivate also takes a PChar as its only parameter, like so:
AppActivate('WindowName') ;
where WindowName is the name of the window that you want to make the current input focus.
Example syntax: SendKeys('abc1{left}def{end}456{left 4}ghi', True) ;
results in a string "abcdefghi123456" being sent to active window (control with focus)

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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