Open and Manage Multiple Word Docs

Posted on 2006-05-18
Last Modified: 2013-12-03

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.

Question by:Mutley2003
    LVL 7

    Accepted 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

    Author Comment

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

    LVL 1

    Expert Comment

    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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    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…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now