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
Solved

Opening a word Doc in a child window

Posted on 2002-04-13
18
477 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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
Convert Jpg /PNG To GIF 5 150
Multiple image collision 13 80
how can i search if string exist in array ? 3 63
Microsoft Access 97 and Delphi XE2 9 54
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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