Solved

Opening a word Doc in a child window

Posted on 2002-04-13
18
466 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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