Board index » delphi » TOleContainer: Replace Server File Menu Functionality - MS Word, Corel WordPerfect

TOleContainer: Replace Server File Menu Functionality - MS Word, Corel WordPerfect

I would like to be able to replicate a subset of the File menu functionality
for MS Word and Corel WordPerfect documents, edited "in place" in a
TOleContainer.  I had hoped to be able to do so using the functionality of
the Server program: for example I want to be able to call up WordPerfects
Print Dialog to print the document.

I have tried a number of combinations of code as follows, in an attempt to
access the interface of the TOleContainer:

procedure TfrmNativeDocumentEditor.actWPPrintExecute(Sender: TObject);
var
  diPerfectScript: IDispatch;
  psInterface: IWPPerfectScript;
begin
  try
    try
      diPerfectScript := IDispatch(olecDocument.OleObjectInterface);
      psInterface := IWPPerfectScript(diPerfectScript);
      psInterface.PrintDlg;
   except
      On E:Exception do
        MessageDlg('A error occured when attempting to print the
documet:'+#10#13+E.Message, mtError, [mbOk], 0);
    end;
  finally
    wpPerfectScript.Disconnect;
  end;
end;

procedure TfrmNativeDocumentEditor.actWPPrintExecute(Sender: TObject);
var
  diPerfectScript: IDispatch;
  psInterface: IWPPerfectScript;
begin
  try
    try
      diPerfectScript := IDispatch(olecDocument.OleObjectInterface);
      psInterface := IWPPerfectScript(diPerfectScript);

      // wpPerfectScript is a PerfectScript TOleServer
      wpPerfectScript.ConnectTo(psInterface);
      wpPerfectScript.PrintDlg;
    except
      On E:Exception do
        MessageDlg('A error occured when attempting to print the
document:'+#10#13+E.Message, mtError, [mbOk], 0);
    end;
  finally
    wpPerfectScript.Disconnect;
  end;
end;

Can anyone help out?

Kevin

 

Re:TOleContainer: Replace Server File Menu Functionality - MS Word, Corel WordPerfect


<<Kevin:
 diPerfectScript :=
IDispatch(olecDocument.OleObjectInterface);

Quote

OleObjectInterface returns an IOleObject interface, and you
cannot cast one interface to another like that. You have to
use the as operator:
 diPerfectScript := olecDocument.OleObjectInterface as
IDispatch;

Hard casts are occasionally possible with interfaces, but
only when you know you already have the interface you want.
For example, you can do this:
 diPerfectScript := IDispatch(olecDocument.OleObject);
because OleObject returns a variant that contains an
IDispatch interface. The hard cast is allowed since what
you have actually is an IDispatch.

You /can/ also do this, because IWPPerfectScript is a
dispinterface (i.e. is an IDispatch interface):

<<Kevin:
 psInterface := IWPPerfectScript(diPerfectScript);

Quote

But you must be quite sure that the object you're accessing
actually implements the IWPPerfectScript interface, or
you'll be in trouble. I don't know Word Perfect at all, but
from the name I'd guess that the IWPPerfectScript interface
has something to do with a scripting capability. The object
in the olecontainer is likely to be a document, i.e.
(guessing from the names) expose the Document or IWPDoc
interface, not the IWPPerfectScript interface.

Try this and see if it works:
var
  Doc: IWPDoc;
  WP: IWPApp;
..
  Doc := IDispatch(OleContainer.OleObject) as IWPDoc;
  WP := Doc.Application;
  wpPerfectScript.ConnectTo(WP.PerfectScript);

If it doesn't, what error messages do you get?

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Other Threads