Hello,
I've trying to connect to a Word or Excel application with Lazarus 0.9.26.
The IDispatch does not work,
I've tried going to the "Invoke" API, and it still doesn't work.
I'm able to create a Word.Application objet, I'm even able to get the
pointer to the "Visible" property.
I just can't set the Visible property to true: I get the error message:
// -2147352572 = DISP_E_PARAMNOTFOUND: parameter not found
Please see attached code.
Does somebody know how to call "Invoke" on IDispatch or better still,
how to call directly methods and properties of Word or Excel objects in
Lazarus?
BR
Thierry
object Form1: TForm1
Left = 332
Height = 300
Top = 158
Width = 400
Caption = 'Form1'
ClientHeight = 300
ClientWidth = 400
ParentFont = False
LCLVersion = '0.9.26'
object Button1: TButton
Left = 96
Height = 25
Top = 70
Width = 75
Caption = 'Test'
OnClick = Button1Click
TabOrder = 0
end
object Memo1: TMemo
Left = 24
Height = 141
Top = 121
Width = 272
Lines.Strings = (
'Memo1'
)
TabOrder = 1
end
end
{ Ceci est un fichier ressource généré automatiquement par Lazarus }
LazarusResources.Add('TForm1','FORMDATA',[
'TPF0'#6'TForm1'#5'Form1'#4'Left'#3'L'#1#6'Height'#3','#1#3'Top'#3#158#0#5'Wi'
+'dth'#3#144#1#7'Caption'#6#5'Form1'#12'ClientHeight'#3','#1#11'ClientWidth'#3
+#144#1#10'ParentFont'#8#10'LCLVersion'#6#6'0.9.26'#0#7'TButton'#7'Button1'#4
+'Left'#2'`'#6'Height'#2#25#3'Top'#2'F'#5'Width'#2'K'#7'Caption'#6#4'Test'#7
+'OnClick'#7#12'Button1Click'#8'TabOrder'#2#0#0#0#5'TMemo'#5'Memo1'#4'Left'#2
+#24#6'Height'#3#141#0#3'Top'#2'y'#5'Width'#3#16#1#13'Lines.Strings'#1#6#5'Me'
+'mo1'#0#8'TabOrder'#2#1#0#0#0
]);
unit FMainTestWord;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
uses Windows, Variants, ComObj, ActiveX;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
const
ServerName = 'Word.Application';
var
Server : OleVariant;
Id : TCLSID;
GuID : TGUID;
aServer : IDispatch;
aResult: HRESULT;
Member: WideString;
lDispID: Integer;
Params: TDispParams;
ExceptInfo : TExcepInfo;
VarResult: OleVariant;
ArgErr : DWord;
LocalVariantArg : array[0..3] of TPROPVARIANT;
dispidNamed : DISPID;
begin
if Assigned(InitProc) then
TProcedure(InitProc);
aServer := nil;
Id := ProgIDToClassID(ServerName);
GuID := ProgIDToClassID(ServerName);
dispidNamed := DISPATCH_PROPERTYPUT;
Member := 'Visible';
ldispID := 0;
FillChar(ExceptInfo, SizeOf(ExcepInfo),0);
FillChar(Params, SizeOf(DispParams),0);
FillChar(LocalVariantArg,SizeOf(LocalVariantArg),0);
Params.rgvarg := @LocalVariantArg;
Memo1.Lines.Clear;
try
//Server := CreateOleObject(ServerName);
//Server := CreateComObject(GuiD);
aResult := CoCreateInstance(GuiD,nil,CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IDispatch, aServer);
Memo1.Lines.Add('CoCreateInstance Result =' +IntToStr(aResult));
//aServer.QueryInterface(IDispatch,Server)
//Server := aServer;
except
Memo1.Lines.Add('Unable to start Word.');
Exit;
end;
//Server.Visible := True; {Make Word visible}
aResult := aServer.GetIDsOfNames(GUID_NULL, @Member, 1, LOCALE_USER_DEFAULT, @lDispID);
Memo1.Lines.Add('GetIDsOfNames on Visible Result =' +IntToStr(aResult));
(* Params.rgvarg[0].VT := varError;
Params.rgvarg[0].scode := DISP_E_PARAMNOTFOUND;
Params.rgvarg[1].VT := varError;
Params.rgvarg[1].scode := DISP_E_PARAMNOTFOUND;*)
// the first parameter is a bool
LocalVariantArg[0].VT := VT_BOOL;
LocalVariantArg[0].boolVal := True;
Params.cArgs := 1;
Params.cNamedArgs := 1;
Params.rgdispidNamedArgs:= @dispidNamed;
aResult := aServer.Invoke( lDispID, GUID_NULL, 0, DISPATCH_PROPERTYPUT, Params,
@VarResult, @ExceptInfo, @ArgErr);
Memo1.Lines.Add('Invoke on Visible Result =' +IntToStr(aResult));
// -2147024809 = COR_E_ARGUMENT: wrong parameters
// -2147352572 = DISP_E_PARAMNOTFOUND: parameter not found
{Open existing document} //Substitute your path and doc
//Server.Documents.Open('c:\Test.doc');
end;
initialization
{$I FMainTestWord.lrs}
end.
_______________________________________________
Lazarus mailing list
[email protected]
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus