How to Add/Remove Favourites in IE through QTP

I recently came across a situation where I needed to write a script that adds a URL to the IE Favourites. I wasn’t able to find a suitable way to do it until I came across a wonderful method which uses VBScript to do the task. Here are the functions that can be used to add/delete favourites respectively :-

Public Function addIEFavourite(favName,URL)

Const ADMINISTRATIVE_TOOLS = 6

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(ADMINISTRATIVE_TOOLS)
Set objFolderItem = objFolder.Self

Set objShell = CreateObject(“WScript.Shell”)
strDesktopFld = objFolderItem.Path
Set objURLShortcut = objShell.CreateShortcut(strDesktopFld & “\”&favName&”.url”)
objURLShortcut.TargetPath = URL
objURLShortcut.Save

Set objShell= Nothing
Set objFolder = Nothing
Set objFolderItem=Nothing
Set objShell = Nothing
Set objURLShortcut = Nothing

End Function

Public Function deleteFavourite(favName)
Set objFSO= CreateObject(“Scripting.FileSystemObject”)
Set WshShell=CreateObject(“Wscript.Shell”)
favPath=WshShell.ExpandEnvironmentStrings(“%USERPROFILE%”)&”\Favorites”

If Instr(favName,”.url”)=0 Then
favName=favName&”.url”
End If

linkDeleted=False

For each objFile in objFSO.GetFolder(favPath).Files
If Trim(Ucase(objFile.Name))=Trim(Ucase(favName)) Then
objFSO.DeleteFile(objFile)
linkDeleted=True
End If
Next

Set objFSO=Nothing
Set WshShell=Nothing

deleteFavourite=linkDeleted
End Function

Here’s how you can add a favourite :-

Call addIEFavourite(“Google”,”http://www.google.com”)

and you can delete it in the following way :-

deleteResult=deleteFavourite(“Google”)

Happy Automating !

 

Advertisements

Eat, Sleep, Automate. REPEAT !

Tagged with: , , , , , , , ,
Posted in Awesum Stuff

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats
  • 168,130 hits
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 796 other followers

%d bloggers like this: