Конференция "Corba" » Не работает dispinterface (WD:= FWordDispIntf.Documents.Add(a,.. [Delphi, Windows]
 
  • Winni (16.10.09 01:42) [0]
    Здравствуйте !  

    Хочу изучить контроллеры автоматизации (для начала на примере Word).
    Есть книги: И.Ю.Баженова Делфи7 и Тейксейра Делфи5, в которых хорошо все описано.
    Однако в моем проекте (текст модуля - ниже) почти не работает dispinterface:
    _ApplicationDisp = dispinterface. Работают присваивания:
          FWordDispIntf.Visible:= true;
          FWordDispIntf.Left:=2;
          FWordDispIntf.Top:=2;
          FWordDispIntf.Width:= 300;
          FWordDispIntf.Height:=200;
          FWordDispIntf.Caption:= 'Word by DispInterface';
    ----
    и не работает
     WD:= FWordDispIntf.Documents.Add(a,b,c,visi);
    (сообщение об ошибке: ... raised exception class EAccessViolation with message
    'Access violation at address 7711CA30 in module oleaut32.dll')
    ----
    причем с основным интерфейсом FWordintf: _Application;
    проблем нет. Доступ через OleVariant (FOle:= CreateOleObject('Word.Application');)
    - тоже работает.

    Подскажите, пожалуйста, в чем тут дело или пошлите на ссылку, где
    можно просветиться. Поиск по 'dispinterface' не помог.
    -------

    { from WinXP.pas:
     WordApplication = _Application;
     _Application = interface(IDispatch)
       property Documents: Documents read Get_Documents;
       function Get_Documents: Documents; safecall;
    -------
     Documents = interface(IDispatch)
       function AddOld(var Template: OleVariant; var NewTemplate: OleVariant):
          WordDocument; safecall;
       function Add(var Template: OleVariant; var NewTemplate: OleVariant;
                    var DocumentType: OleVariant; var Visible: OleVariant):
          WordDocument; safecall;
    ==============================

     _ApplicationDisp = dispinterface
       property Documents: Documents readonly dispid 6;
    }

    unit Unit1;

    interface

    uses
     Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs, ActiveX, WordXP, StdCtrls, ComCtrls, ExtCtrls, ExtDlgs;

    type
     TForm1 = class(TForm)
       btnRunServ: TButton;
       btnNewDoc: TButton;
       btnShowHide: TButton;
       RG1: TRadioGroup;
       procedure btnRunServClick(Sender: TObject);
       procedure btnNewDocClick(Sender: TObject);
       procedure btnShowHideClick(Sender: TObject);
       procedure FormCreate(Sender: TObject);
       procedure RG1Click(Sender: TObject);
     private
       { Private declarations }
       FWordintf: _Application;
       FWordDispIntf:   _ApplicationDisp;
       FVari: integer;
     public
       { Public declarations }
     end;

    var
     Form1: TForm1;
     Wapp: WordApplication = nil;  WD: WordDocument = nil;

    implementation uses ComObj;

    {$R *.dfm}
    var lada: integer;

    procedure TForm1.btnRunServClick(Sender: TObject);
    var ii: integer;
    begin
    try
    case FVari of
     0: begin
          FWordintf:= CoWordApplication.Create;
          FWordintf.Visible:= true;
          FWordintf.Left:=2;
          FWordintf.Top:=2;
          FWordintf.Width:= 400;
          FWordintf.Height:=300;
          FWordintf.Caption:= 'Word by interface';
     end;
     1: begin
          FWordDispIntf:= CreateComObject(CLASS_WordApplication)
            as _ApplicationDisp;
          FWordDispIntf.Visible:= true;
          FWordDispIntf.Left:=2;
          FWordDispIntf.Top:=2;
          FWordDispIntf.Width:= 300;
          FWordDispIntf.Height:=200;
          FWordDispIntf.Caption:= 'Word by DispInterface';
     end;
    end;
    except
    end;
    end;

    procedure TForm1.btnNewDocClick(Sender: TObject);
    var visi,a,b,c: OLEvariant; ii: integer;
    begin
     visi:= true;
     a:= EmptyParam;
     b:= false;
     c:= wdNewBlankDocument;

     case FVari of
     0: if FWordintf <> nil then
       WD:= FWordintf.Documents.Add(a,b,c,visi);
     1: if FWordDispIntf <> nil then
       WD:= FWordDispIntf.Documents.Add(a,b,c,visi);
     end{case};
    end;

    procedure TForm1.btnShowHideClick(Sender: TObject);
    begin
     case FVari of
     0: if FWordintf <> nil then
       FWordintf.Visible:= not FWordintf.Visible;
     1: if FWordDispIntf <> nil then
       FWordDispIntf.Visible:= not FWordDispIntf.Visible;
     end{case};
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
     Fvari := 0;
    end;

    procedure TForm1.RG1Click(Sender: TObject);
    begin
     FVari := RG1.ItemIndex;
    end;

    end.
 
Конференция "Corba" » Не работает dispinterface (WD:= FWordDispIntf.Documents.Add(a,.. [Delphi, Windows]
Есть новые Нет новых   [118695   +32][b:0][p:0]