Solved

Opening a word Doc in a child window

Posted on 2002-04-13
18
471 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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