Solved

Opening a word Doc in a child window

Posted on 2002-04-13
18
453 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
  • 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

21 Experts available now in Live!

Get 1:1 Help Now