Solved

Opening a word Doc in a child window

Posted on 2002-04-13
18
488 Views
Last Modified: 2010-04-04
Hello All -

I have a MDI application and need to open a Word Doc in a child window. Here is what I have so far:

procedure TMainForm.CreateMDIChild(const Name: string);
var
  Child: TMDIChild;
begin
  { create a new MDI child window }
  Child := TMDIChild.Create(Application);
  Child.Caption := Name;
  if FileExists(Name) then Child.Memo1.Lines.LoadFromFile(Name);
end;


procedure TMainForm.FileOpen1Execute(Sender: TObject);
begin
  if OpenDialog.Execute then
    CreateMDIChild(OpenDialog.FileName); //reports.doc is file name
if(ShellExecute(HInstance, 'Open', 'reports.doc', '', '', SW_SHOWNORMAL)= ERROR_FILE_NOT_FOUND)
then
   begin
    MessageDlg('Unable to find file: reports.doc' + chr(13) +
      'Please launch it from the disk Location.', mtWarning,[mbOk],0);
    end;
end;

which only opens Word in another process and fails to attach itself to a child form in my app. Do I need to a OLE Variant like this:

uses OleAuto;

var
  MSWord : Variant;
begin
  MsWord := CreateOleObject('Word.Basic');
  MsWord.FileNewDefault;
end;

Or what?

TIA-

Jim
0
Comment
Question by:JPersinger
[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
  • 8
  • 4
  • 3
  • +3
18 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 6939117
listening
0
 
LVL 17

Expert Comment

by:geobul
ID: 6941389
Hi,
Create an empty child form TMDIChildDoc.

procedure TMainForm.CreateMDIChildDoc(const Name: string);
var
 Child: TMDIChildDoc;
 Container: TOLEContainer;
begin
 { create a new MDI child window }
 Child := TMDIChildDoc.Create(Application);
 Child.Caption := Name;
 Container := TOLEContainer.Create(Child);
 Container.Parent := Child;
 Container.AutoActivate := aaGetFocus;
 Container.Align := alClient;
 Child.Container.CreateObjectFromFile(Name, False);
 Child.Show;
end;

procedure TMainForm.FileOpen1Execute(Sender: TObject);
begin
 if OpenDialog.Execute then
   if FileExists(OpenDialog.FileName) then begin
     CreateMDIChildDoc(OpenDialog.FileName);
   end else begin
     MessageDlg('Unable to find file: reports.doc' + chr(13) +
     'Please launch it from the disk Location.', mtWarning,[mbOk],0);
   end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 6941395
Forgot to say: add 'OleCtnrs' in your uses clause.

Regards, Geo
0
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!

 

Author Comment

by:JPersinger
ID: 6941755
That loaads fine, but if the document is over a page long, how do I get scroll bars to show up?

I tried placing the OLE container inside a scroll box, but that failed too.

Jim
0
 
LVL 17

Expert Comment

by:geobul
ID: 6941933
The line:
Container.AutoActivate := aaGetFocus;
should do that. I see scroll bars on my form. By default you have to double-click on it.

Regards, Geo
0
 

Author Comment

by:JPersinger
ID: 6942129
Well - now I've done it. Started a new app to test where I went wrong the last time, and I get an error message " Opertation not allowed on an empty OLE container." What have I done now?

Jim
0
 

Author Comment

by:JPersinger
ID: 6943351
I got past the error - but still no scroll bars..:(

Jim
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6943584
Put an TOleContainer on the form.

To load a word doc do something like:

  OleContainer1.AllowInPlace := True;
  OleContainer1.AutoActivate := aaGetFocus;
  OleContainer1.CreateObjectFromFile('c:\test.doc', False);


To save the doc:

  OleContainer1.SaveAsDocument('c:\test.doc');


You should get the full word functionality.

0
 

Author Comment

by:JPersinger
ID: 6943608
I still seem to be doing something wrong. The DOC opens up in Word and not in my frame within the application. Before I was able to load the file in my frame, but I didn't get the scroll bars.

Here is a copy of the code:

procedure TMainForm.CreateMDIChildDoc(const Name: string);
var
Child: TMDIChildDoc;
Container: TOLEContainer;
begin
{ create a new MDI child window }
Child := TMDIChildDoc.Create(Application);
Child.Caption := Name;
Container := TOLEContainer.Create(Child);
Container.Parent := Child;
Container.AllowInPlace := True;
Container.AutoActivate := aaGetFocus;
Container.Align := alClient;
Child.Container.CreateObjectFromFile(Name, False);
Child.Show;
end;

My DOC should show up in the frame, correct?

Jim
0
 

Expert Comment

by:TomazB
ID: 6945839
Listening
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6946160
is this set?

  OleContainer1.AllowActiveDoc := True;
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6946166
I get the word doc with scroll bars etc. in the OleContainer and the word toolbars occupy the top of the form above the OleContainer.
0
 

Author Comment

by:JPersinger
ID: 6946330
Steve -

Yes. Maybe someone should post the entire code copy I can copy and paste so I can test it?

Jim
0
 

Author Comment

by:JPersinger
ID: 6946388
Weird -

I fired up Delphi on another machine and it work. Not really the way it was planed too, but worked. Maybe I wasn't to clear on my objective or two machines are having the same problem. Here's what I observed:

When creating a 'wizzard' version MDI application, and removing the TMemo file from the client piece I replaced it with a container. I then assigned the container (by right clicking on it), my Word file - prodiving a full path.

I then ran the application and open my document from the tool bar. My file was displayed but only by the width/ height of the form - and not complete. When I double clicked on the child form, Word opened up INSIDE my applcation. From here I could see the entier file (complete with scroll bars, print functions, and save options, etc).

Since this gives the user full access to the file, they could change the files contents. (Not sure thats an issue or not). I do note that geobul pointed out the double clicking issue, but I'd really like the 'child form' to control the document size, providing a scroll bars (maybe putting the container in a scroll box).

Is there a way for the form to control the size of the document and not to allow the file to open up Word - unless word is with the form ONLY.

If there isn't a way to get around that issue - please let me know. So that fact, geobul would be awarded the points since he indicated the double clicking issue and I may of misunderstood him from the start.

Jim


0
 
LVL 17

Accepted Solution

by:
geobul earned 75 total points
ID: 6947051
Hi Jim,

You already have a child form with an OLEContainer on it. Its properties are default. Try the following:

procedure TMainForm.CreateMDIChildDoc(const Name: string);
var
  Child: TMDIChildDoc;
begin
  { create a new MDI child window }
  Child := TMDIChildDoc.Create(Application);
  Child.Caption := Name;
  Child.OLEContainer1.CreateObjectFromFile(Name, False);
  Child.OLEContainer1.AutoActivate := aaGetFocus;
  keybd_event(VK_RETURN, 0, 0, 0);
  keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
end;

procedure TMainForm.FileOpen1Execute(Sender: TObject);
begin
  if OpenDialog.Execute then
    CreateMDIChildDoc(OpenDialog.FileName);
end;

Regards, Geo
0
 
LVL 1

Expert Comment

by:xsoft
ID: 6956828
listening...
0
 

Author Comment

by:JPersinger
ID: 6956915
That did it!

To all -
Thanks for working me on this issue....:)

Jim
0
 

Author Comment

by:JPersinger
ID: 6958367
Geo

Can the same method be used with Frames Application instead of a MDI application?

Jim
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
When I am typing into a TEdit last word repeats 7 179
Correct Component for Shopping Cart. 2 136
Magic Software info 18 156
JAudiorecorder record freezing the app 29 93
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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