Board index » delphi » Scott Samet [Team B] - A previous post of yours....

Scott Samet [Team B] - A previous post of yours....

This post below is from 11/28/97.  If you could also send me the
code to implement the GetDispIDsofName It would be very helpful!

Jason T. Ashmead
mailto:j...@ash.net

Quote
Scott Samet [TeamB] wrote:

> Sorry, I missed the IDispatch.  Try this; it compiles, but I have no
> way to test it.

> Procedure ReadSource(Progress: IDispatch);
> var
>   OLE:  OLEVariant;
> begin
>   OLE := Progress;
>   OLE.SetAppText ('Something');
> end;

> If it does not work, I have general purpose code that does a
> GetDispIDsOfName and an Invoke to set a property when the name is not
> known at compile time.  It's far more complex (100 lines or so).

 

Re:Scott Samet [Team B] - A previous post of yours....


I'd really like to see the property setting one too -- I am having trouble
with this right now, maybe I could figure out why from your example!

-Peter

Quote

> Scott Samet [TeamB] wrote:
> > If it does not work, I have general purpose code that does a
> > GetDispIDsOfName and an Invoke to set a property when the name is not
> > known at compile time.  It's far more complex (100 lines or so).

Re:Scott Samet [Team B] - A previous post of yours....


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    MsWord:   OLEVariant;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Uses ActiveX, ComObj;

ResourceString
  SNoMethod = '''%s'' not supported by automation object';

Function GetPropDispID (Dispatch: IDispatch;
                        Const PropName: WideString): Integer;
var
  Status:     Integer;
  PPropName:  PWideChar;
begin
  PPropName := PWideChar (PropName);
  Status := Dispatch.GetIDsOfNames (GUID_NULL, @PPropname, 1,
                                    GetThreadLocale, @Result);
  if Status = DISP_E_UNKNOWNNAME then
    raise EOleError.CreateFmt(SNoMethod, [PropName])
  else
    OleCheck(Status);
end;

Function GetAutoObjProp (Obj: IDispatch;
                         Const PropName: WideString): OLEVariant;
Const
  DispParams: TDispParams = (rgvarg: Nil; rgDispIDNamedArgs: Nil;
                             cArgs: 0; cNamedArgs: 0);
var
  Status:     Integer;
  ExcepInfo:  TExcepInfo;
begin
  Status := Obj.Invoke (GetPropDispID (Obj, PropName),
                        GUID_NULL, 0,
                        DISPATCH_METHOD or DISPATCH_PROPERTYGET,
                        DispParams, @Result, @ExcepInfo, nil);
  if Status <> 0 then
    DispatchInvokeError (Status, ExcepInfo);
end;

Procedure SetAutoObjPropValue (Const Obj: IDispatch;
                               PropName: WideString; Const Value:
Variant);
const
  DispIDs: Integer = DispID_PropertyPut;
var
  Status:     Integer;
  ExcepInfo:  TExcepInfo;
  Arg:        TVariantArg;
  CValue:     Variant;
  DispParams: TDispParams;
begin
  If VarType (Value) = varString then
    VarCast (CValue, Value, varOLEStr)
  else
    CValue := Value;
  Arg.vt      := varVariant or varByRef;
  Arg.pVarVal := @CValue;
  DispParams.rgvarg := @Arg;
  DispParams.rgdispidNamedArgs := @DispIDS;
  DispParams.cArgs := 1;
  DispParams.cNamedArgs := 1;
  Status := Obj.Invoke (GetPropDispID (Obj, PropName),
                        GUID_NULL, 0, DISPATCH_PROPERTYPUT,
                        DispParams, Nil, @ExcepInfo, Nil);
  if Status <> 0 then
    DispatchInvokeError(Status, ExcepInfo);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MsWord := CreateOleObject('Word.Application');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Vis:      Boolean;
begin
  Vis := GetAutoObjProp (IDispatch(MsWord), 'Visible');
  SetAutoObjPropValue (IDispatch(MsWord), 'Visible', Not Vis);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  GetAutoObjProp (IDispatch(MsWord), 'NonsenseProperty');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MsWord.Quit;
end;

end.

Other Threads