I have written a DLL in VB (ObjFactoryLib.DLL - attached)
containing class ObjFactory (source attached)
with 3 methods (declarations in VB syntax):
Public Function CreateInstance(ByVal ProgId As String, [ByVal Server As
String]) As Object
Public Function GetAttr(ByVal obj As Object, ByVal attrname As String, ByVal
resultisobject As Boolean)
Public Sub SetAttr(ByVal obj As Object, ByVal attrname As String, ByVal
value)
CreateInstance() creates an object of the class, given by "ProgId"
parameter,
which is the registered name of the class (e.g. "Word.Application").
It uses VB's CreateObject function, this way it will automatically find the
current version.
GetAttr() and SetAttr() are required as OpenROAD's GetAttribute/SetAttribute
methods didn't work.
The "resultisobject" parameter of GetAttr() is required to define if the
result is an object
or a scalar (integer, varchar, ...).
Methods are invoked dynamically using the methodname in a varchar variable.
You of course have to register ObjFactoryLib.DLL (REGSVR32
and declare it as External Class Library in OR (component name
"ObjFactoryLib"),
but then you can create all COM objects (Word, Excel, ...) using this one.
Example OpenROAD code:
initialize()=
declare
objfact = ObjFactory;
app = _ObjFactory DEFAULT NULL; // The Word Application object
methodname = VARCHAR(32) NOT NULL; // The method to invoke
Docs = _ObjFactory DEFAULT NULL; // The Documents of the word
application
Doc = _ObjFactory DEFAULT NULL; // The opened word document
enddeclare
{
CurFrame.Trace(text = 'Start.');
}
ON CLICK start_btn =
{
IF app IS NOT NULL
THEN
// Document already opened -> just Show it normal and activate it
objfact.SetAttr(app, 'WindowState', 0); // Normal
methodname = 'Activate';
app.:methodname();
ELSE
app = objfact.CreateInstance('Word.Application');
Docs = objfact.GetAttr(app, 'Documents', TRUE);
methodname = 'Open';
Doc = Docs.:methodname('C:\temp\MyDoc.doc');
objfact.SetAttr(app, 'Visible', TRUE);
ENDIF;
}
ON CLICK stop_btn =
{
IF app IS NOT NULL
THEN
methodname = 'Quit';
app.:methodname();
Doc = NULL;
Docs = NULL;
app = NULL;
ENDIF;
}
Hope this helps,
Bodo.
Bodo Bergmann
Senior Architect
Fintechnix Pty Ltd
Level 3, 55 Clarence Street, Sydney, NSW 2000
P.O. Box H153, Australia Square, Sydney, NSW 1215
Phone +61 2 8234 8200
web www.fintechnix.com
email Bodo.B...@fintechnix.com
-----Original Message-----
From: openroad-us...@peerlessit.com
[mailto:openroad-us...@peerlessit.com]On Behalf Of Gareth
Edwards
Sent: Monday, 20 February 2006 10:30 PM
To: openroa...@peerlessit.com; Pete.Wi...@vivista.sungard.com
Subject: Re: [Openroad-users] External Classes - Success!
Well done on that one Pete. I'll wouldn't mind a peak at that app that you
mentioned. That's gonna come in very handy when we come to testing with Word
2003.
Cheers,
Gareth Edwards
=============
Paisley University
x.3748
>>> Pete.Wi...@vivista.sungard.com 17/02/2006 16:54:39 >>>
Many,many thanks to the those that have contributed ideas on this subject,
and after much tinkering I appear to have finally managed to create an
External Class dynamically & use it!
Detecting Classes:
I tried various methods, none really 100% what I'd like to have. I tried
the 4gl, and also the 'TypeLib Information' External Class, but in the end
went for looking at the registry, as this is what i suspect the 4gl does
anyway.
I have used code borrowed from Doug White, and supplied here as an
application export for anyone else:
http://www.peerlessit.com/pipermail/openroad-users/attachments/20050502/d8da
6a33/registry.obj
All External Classes (TypeLibs) are listed here :
HKEY_CLASSES_ROOT\TypeLib\
The ID for Word is {00020905-0000-0000-C000-000000000046}, and the
sub-directories 8.1, 8.3 etc are the different versions of Word, 8.1 = Word
2000, 8.3 = Word 2003. So I simply check for the existance or not, of these
registry entries. The first value (8) is the Major Version, the second is
the Minor Version of the external library. Now I can build my external
class ...
Creating Classes
In the end it was simple, using the 'ExtLibSource' class
....
wv_ExtLibSource = ExtLibSource;
....
/* create External Class & attach it to the running application */
wv_ExtLibSource.uniqueid = '{00020905-0000-0000-C000-000000000046}';
wv_ExtLibSource.MajorVersion = 8;
wv_ExtLibSource.MinorVersion = 1; /* change this for each version of Word
*/
wv_extlibsource.Name = 'myWord';
wv_extlibsource.ParentApplication =
CurProcedure.ObjectSource.ParentApplication;
end
Now the external library exists, but to access it we need to use dynamic
stuff so we can get it past the compiler. You could use Dynamic
Expressions, something like this :
....
wv_word = ExtClass;
....
wv_dexp = CurProcedure.Scope.CreateDynExpr ( string = 'myWord!Application'
);
wv_dexp.GetValue ( value = byref(wv_word) );
wv_dexp = CurProcedure.Scope.CreateDynExpr ( string =
'wv_word.WindowState' );
wv_dexp.SetValue ( value = 2 );
But it would take a lot of rewriting of my code, so what I have done is
create a 4gl procedure on the fly, and again attach it to the current
application. This 4gl proc contains my existing code, which will reference
the 'myWord' external class. As you will see I have picked up my 4gl code
from a file, but it could be stored in the database as a StringObject.
As the 4gl proc does not exist until after I have created the external
class, all is ok, and I can call the 4gl proc using a dynamic string like so
:
....
wv_procsource = Proc4glSource;
....
wv_proc_v = 'myProc';
wv_procsource.Script = StringObject.Create();
wv_procsource.Script.FileHandle = 'c:\myProc.4gl';
wv_procSource.Name = wv_proc_v;
wv_procsource.ParentApplication =
CurProcedure.ObjectSource.ParentApplication;
CALLPROC :wv_proc_v;
And 'myProc.4gl' looks like this :
procedure myProc() =
declare
wv_word = myWord!Application;
wv_doc = myWord!Document;
enddeclare
begin
wv_word.WindowState = 2;
wv_doc = wv_word.Documents().Open('c:\hello.doc',,1,,,,,,,,,);
wv_doc.PrintOut(0,,,,,,,1,,,,,,,,,,);
wv_doc.Close(0);
wv_doc = null;
wv_word = null;
end;
And yes it works! I will knock together a small app for any that want it,
just let me know
Cheers - Pete
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.
www.mimesweeper.com
**********************************************************************
This message has been checked for all known viruses on behalf of SunGard
Vivista by MessageLabs.
http://www.messagelabs.com or Email: mailswee...@vivista.sungard.com
For further information http://www.sungard.com/vivista
_______________________________________________
Openroad-users mailing list
Openroa...@peerlessit.com
http://www.peerlessit.com/mailman/listinfo/openroad-users
Legal disclaimer
--------------------------
The information transmitted is the property of the University of Paisley and
is intended only for the person or entity
to which it is addressed and may contain confidential and/or privileged
material. Statements and opinions expressed in this
e-mail may not represent those of the company. Any review, retransmission,
dissemination and other use of, or taking
of any action in reliance upon, this information by persons or entities
other than the intended recipient is prohibited.
If you received this in error, please contact the sender immediately and
delete the material from any computer.
--------------------------
_______________________________________________
Openroad-users mailing list
Openroa...@peerlessit.com
http://www.peerlessit.com/mailman/listinfo/openroad-users
Specialist providers of back and front office systems for the financial
services industry.
Featuring: Fintechnix®
Disclaimer:
Notice: This message contains privileged and confidential information
intended only for the use of the addressee named above. If you are not the
intended recipient of this message you are hereby notified that you must not
disseminate, copy or take any action in reliance on it.
Any views expressed in this message are those of the individual
sender,except where the sender specifically states them to be the views of
Fintechnix Pty Ltd.