Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delphi tOLEContainer CreateOLEObject LoadFromFile - gives Invalid Stream Format with HTML file

Posted on 2011-02-14
8
Medium Priority
?
3,886 Views
Last Modified: 2012-05-11
Try the following steps:

1) Open MS Word and type "Hello World"
2) Save this document as an HTML WebPage file (eg C:\HelloWorld.HTM)
3) In Delphi create a form with a TOLEContainer and add the following code to a Button

    OleContainer1.CreateObject('Word.Document',false);
    OleContainer1.LoadFromFile('C:\HelloWorld.HTM');

The second line gives the error "Invalid Stream Format".  Why?

Note: If you save the "Hello World" as a word document it works fine.

How can I get a tOLEContainer to create an instance of MS Word and then load an HTML file for editing?

Surely this can be done - I mean MS Outlook does it.

If you can answer this question then there are more points to be found here...
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26808908.html 

I am hoping that someone with a little Win API and OLE knowledge can solve this one for me.

I really want to use tOLEContainer or some derivitive or equivalent WinAPI calls.
0
Comment
Question by:enigmasolutions
[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
  • 6
8 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34887892

Dont use lodafromfile unless it was saved from the OleContainer

use

OleContainer1.CreateObjectFromFile('C:\HelloWorld.HTM', False);
0
 
LVL 1

Accepted Solution

by:
enigmasolutions earned 0 total points
ID: 34894039
ewangoya,

Thank you.  So loadfromfile doesn't work by loading a file. That helps a bit.  Also the CreateObjectFromFile is part of teh answer.

Anyway we have found a solution.  It is a bit ugly - we had to save the HTML file with a .Doc extension first.  Then we could use CreateObjectFromFile.  (So in the steps at the very top of this question you jus tneed a fourth step to rename the .HTM file as a .DOC file)

Then the code below did the job.
//make sure teh HTML file has a .doc extension
OleContainer1.AutoActivate:=aaManual;
OleContainer1.CreateObjectFromFile('c:\HelloWorld.doc',False);

Open in new window

0
 
LVL 1

Author Comment

by:enigmasolutions
ID: 34894166
I have attached a sample application that does HTML emailing using MS Word as the editor within a Delphi form.
EmailerWordApp.zip
0
Technology Partners: 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!

 
LVL 1

Author Comment

by:enigmasolutions
ID: 34894185
Here is a challenge:

Can anyone, work out a way of doing it without renaming the file?
What about not creating the file at all - just loading a stream with the HTML in it?

I doubt anyone will be able to do either of these.

Anyway I will leave the question open for a week - then close it and award points.

0
 
LVL 24

Expert Comment

by:jimyX
ID: 34903882
From Delphi help files:
"Use the OleContainer component to provide your application with the ability to link and embed objects from an OLE server."
"When you activate an object inside the OLE container, control transfers to the OLE server application, so the user can access all the functionality of the server application from within your container application."
"OleContainer can create either an embedded or linked OLE object to an external application."

I am not sure if it is possible to have HTML files edited on OleContainer. But if you want to have the ability to work on the HTML file then you can use Memo to handle the HTML and link it to WebBrowser.
//http://delphi.about.com/od/twebbrowser/a/save-as-mht.htm
procedure WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ;
 var
   PersistStream: IPersistStreamInit;
   Stream: IStream;
   FileStream: TFileStream;
 begin
   if not Assigned(WB.Document) then
   begin
     ShowMessage('Document not loaded!') ;
     Exit;
   end;

   PersistStream := WB.Document as IPersistStreamInit;
   FileStream := TFileStream.Create(FileName, fmCreate) ;
   try
     Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
     if Failed(PersistStream.Save(Stream, True)) then ShowMessage('SaveAs HTML fail!') ;
   finally
     FileStream.Free;
   end;
 end; (* WB_SaveAs_HTML *)

// http://delphi.about.com/cs/adptips2004/a/bltip0104_4.htm
procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
   sl: TStringList;
   ms: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank') ;
   while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
    Application.ProcessMessages;

   if Assigned(WebBrowser.Document) then
   begin
     sl := TStringList.Create;
     try
       ms := TMemoryStream.Create;
       try
         sl.Text := HTMLCode;
         sl.SaveToStream(ms) ;
         ms.Seek(0, 0) ;
         (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
       finally
         ms.Free;
       end;
     finally
       sl.Free;
     end;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.LoadFromFile('Hello World.htm');
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin
  WBLoadHTML(WebBrowser1,Memo1.Text) ;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  WB_SaveAs_HTML(WebBrowser1,'Hello World.htm') ;
end;

Open in new window

DelphiHTML.zip
0
 
LVL 1

Author Comment

by:enigmasolutions
ID: 34977192
JimmyX,

Thank you.  I got opening HTML in a tWebBrowser (see also my earlier post & attached sample application).  

I still have no better solution for opening it in a tOLEContainer (apart from my hack solution - which isn't too bad).

0
 
LVL 1

Author Comment

by:enigmasolutions
ID: 34978527
How do I award points to contributors - yet record my answer as the correct one?
0
 
LVL 1

Author Closing Comment

by:enigmasolutions
ID: 35005385
This is the best solution.  I will try to award points to the other experts fro their iinput.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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