Today we’re gonna look at QTP’s internal mechanism for creating singleton COM objects, called Reserved Objects; And we’ll see how Reserved Objects can be used to improved and simplify our scripts.
Well, that was a heavy title… let’s see if we can break it down:
1. A COM object is any object you can create via CreateObject command (Scripting.Dictionary, Word.Application, etc.)
2. Singleton means that only a single instance of the reserved object can be created.
3. Internal mechanism means that if you’re gonna use it, it will include some registry tweaking.
Now that we’ve simplify things a bit, let’s move on.
QTP has a neat mechanism called Reserved Objects. Any COM object can be made into a Reserved Object, and will be accessible via QTP from there on.
So, why not create that object via CreateObject method, assign it to a variable, and just use it regularly? Reserved Objects have two major benefits:
1. They are created ONCE when a QTP script starts running, and are globally accessible. So they’re kinda like declaring a global variable, but in a much stronger, deeper sense. It can solve may of the pesky problems of unclosed system processes due to unclosed CreateObjects objects
2. Once an object is added to the reserved objects pool, it’s accessible via QTP’s auto-complete. This means that both the object’s name, and internal methods & properties will be auto-completed. Which is kinda cool.
Reserved Object can be used to simplify and organize many tasks: file IO, log writing, using excel / word / outlook etc. I specifically love using Reserved Objects to replace the Environment object.
The environment object is supposed to give you a global data storage, but I just find it too restrictive. It can only house simple variable types, which means you can forget about passing objects globally; There is no simple way for looping through all the values; And many more annoying "features".
So, instead of using the Environment object, I create my own Scripting.Dictionary reserved object (as seen in the screen captures), which allows me to host any number of sub-values, ranging from a string, a QTP object, a nested dictionary, to just about anything. I can then loop through the values by numbers, keys, or items, and have complete freedom in my code. I know it’s kinda hard to notice, but I REALLY love the dictionary object…
So, how do we actually create reserved object?
This part is all about editing the registry. AdvancedQTP takes no direct or indirect responsibility registry tweaking. Do this at your own risk, and you better know what you’re doing…
But ’nuff chit-chat.
Here’s the thing: Open your registry and go to :
HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\
There you’ll see many of QTP’s internal object. DO NOT MESS WITH THEM.
Next, create a new key, with the name of the reserved object (e.g. Advanced QTP)
Enter the key "folder", and create these values :
ProgID (string) - Holds the COM program ID we want to create (e.g. Scripting.Dictionary)
VisibleMode (dWord) - Holds the value 2. This controls the auto-complete and intellisense appearance.
UIName (string) - The name that will refer to the reserved object. Make sure this is the same as the key name.
There are more properties, which control the icon of the object, and other options. Sadly, I while trying to figure them out, I’ve messed my QTP a bit, so I’ve stopped experimenting. If you know of more properties, please let me know.
And that’s it. The next time you’ll open QTP, the reserved object will be accessible through the auto-complete from all the scripts and functions.
Have fun :)
Posted in QTP Hacks


Yaron Assa




March 30th, 2008 at 7:45 pm
[…] a previous article, I’ve discussed (among other things) the possibility of adding COM objects to the QTP reserved […]
April 16th, 2008 at 7:44 am
Its really great and working fine.I just cant stop my self saying thanks.I have been searching for the same kind of thing for many months and i could not get it.
But advacedQTP really cleared my doubt.I just have no words to express my thanks
April 16th, 2008 at 8:32 am
You’re very welcomed
May 21st, 2008 at 11:53 am
very nice!
June 4th, 2008 at 7:53 am
Hats off to you guys….
June 4th, 2008 at 8:48 am
I must say its g8 solution. Thanx a lot..
June 4th, 2008 at 10:13 am
I tried creating “Excel.Application” com object with the properties same as you had mentioned ,but the value i had used for ProgID is Excel.Application.
I tried the following code :
Set objExcel = AdvancedQTP
Set objWorkBook = objExcel.Workbooks.Open(”c:\automation\Fstar_Info.xls”)
MsgBox objExcel.Sheets.Count
objExcel.Quit
It was throwing error saying object doesnot support this property for the second line of code.
Please help me out with this issue.
Thanks in advance for your help.
June 4th, 2008 at 10:27 am
Hi…It started working after i re-launched QTP…it is such a wonderful learning for me.Thank you very much for posting such a wonderful article
June 19th, 2008 at 6:03 am
[…] series of QTips covering it’s basic uses, as well as some advanced articles of using it as a reserved global dictionary, a parameter storage for generic functions (here and here), and […]
June 19th, 2008 at 8:20 am
Great work thanx
June 25th, 2008 at 1:51 pm
Its really Wonderful. Thanks a lot for such useful posts…
July 7th, 2008 at 11:23 am
Hi,
This is a great option, but i would like to understand how is QTP picking up the functions once it is added to the registry.
I tried to create a .qfl file and added in the registry and i got all the functions in the .qfl.
But even after i deleted the .gfl file closed and open QTP i am able to get the functions. How is this possible. Can you please explain more.
July 9th, 2008 at 7:35 am
Hats oFF……
July 30th, 2008 at 6:01 pm
Really its Excellent.. Its a greate learning.
September 22nd, 2008 at 2:21 pm
great man its works i create for excel com object but when i run this its runs only one time after that it gives an error
” The remote server machine does not exist or is unavailable “
September 22nd, 2008 at 4:01 pm
Sorry it working for excel object but give error for word object
see the below
” ObjMsWord i created in registory for word object ‘Its working fine when it run first time if i tried to ‘ run again it gives an error ” The remote server ‘machine does not exist or is unavailable “
set Doc = ObjMsWord.Documents.add( )
‘print “Error # ” & CStr(Err.Number) & ” ” & Err.Description
ObjMsWord.Visible =true
Set selection = ObjMsWord.Selection
‘print “Error # ” & CStr(Err.Number) & ” ” & Err.Description
With selection
.Font.Name = “Arial”
.Font.Size = “18″
.TypeText “Network Adapter Report”
.TypeParagraph()
.Font.Size = “14″
.TypeText ” rajiv ” & Date()
.TypeParagraph()
.TypeParagraph()
.Font.Size = “10″
End With
‘print “Error # ” & CStr(Err.Number) & ” ” & Err.Description
Doc.saveas “c:\rajiv.doc”
Doc.Application.Quit
Set selection = nothing
Set Doc = nothing
set ObjMsWord =nothing
October 1st, 2008 at 9:44 pm
Its very much useful. Good job.
October 10th, 2008 at 9:25 am
very good article.
March 28th, 2009 at 2:47 pm
Seems to be a great article, but while implementing it i could not find any any path like “Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\ “. can anybody guide me.
Thanks in advance.
July 1st, 2009 at 5:30 am
hegupta
try HKEY_CURRENT_USER
August 26th, 2009 at 8:49 am
Thanks Daniva .. its working.
August 26th, 2009 at 9:12 pm
The values in the dictionary seem to only persist within one given test and cannot be shared globally by QTP within a new test (or between components). Does anyone have input on how to share the dictionary between tests/components?