I've been exploring options for expanding my QuickTest Professional scripting capabilities, and came across this article this morning, so I decided to experiment a bit. The code below works fine when executed inside the QTP environment, but I could see a use for this outside of the QTP environment as well. Unfortunately, it is causing an error when run from a stand-alone .vbs file
Set MyDate = DotNetFactory.CreateInstance("System.DateTime").Now
msgbox MyDate.ToShortDateString()
The error is "Object Required: 'DotNetFactory'"
I've made .Net calls from VBScript before, but none of them have used DotNetFactory. Code such as
Set coll = CreateObject开发者_StackOverflow("System.Collections.Queue")
Does not cause an error.
Do I need to set a reference to DotNetFactory? The text from the article
We use ‘System.DateTime’ as type name. We do not need to specify the assembly for this as it belongs to the already loaded namespace ‘System’ (mscorlib.dll).
makes me think so, because nothing is automatically loaded by my script editor. If so, how do I do this? I am not using an IDE, just a text editor, so any references would have to be loaded by the script itself.
Update: As pointed out by Motto, it can't be done without some extra work. The quote from the article was pointing out that System.DateTime
not DotNetFactory
is included in mscorlib.
AFAIK DotNetFactory
is an object created by QTP, not part of mscorelib as you said in a comment to Mikeb's answer. Therefore you can't access it from a stand along VBS file unless QTP has exposed a prog-id.
Can you create a DotNetFactory, as in :
Set dnf = CreateObject("Qualified.Name.To.DotNetFactory")
dnf.CreateInstance("System.DateTime")
精彩评论