2011-09-18

Create an MSIE Favorite using VBA & WSH

A little sub-routine to create MSIE Favorites for MS-Office users. It uses Windows Scripting Host (WSH) which has a shell object that has functions to find a computer's Favorites folder (SpecialFolders()) and create a URL shortcut (CreateShortcut()).


Option Explicit

'Add references:
'1. Microsoft Scripting Runtime (Scripting namespace)
'2. Windows Script Host Object Model (IWshRuntimeLibrary namespace)

Public Sub CreateFavorite(ByVal strName As String, ByVal strUrl As String)
  Dim fso As Scripting.FileSystemObject
  Set fso = CreateObject("Scripting.FileSystemObject")
  
  Dim wshShell As IWshRuntimeLibrary.wshShell
  Set wshShell = CreateObject("Wscript.Shell")
  
  Dim strFavorites As String
  strFavorites = wshShell.SpecialFolders("Favorites")
  
  Dim shortcut As IWshRuntimeLibrary.WshURLShortcut
  Set shortcut = wshShell.CreateShortcut(fso.BuildPath(strFavorites, strName & ".url"))
  shortcut.TargetPath = strUrl
  shortcut.Save
  
  Set shortcut = Nothing
  Set wshShell = Nothing
  Set fso = Nothing
End Sub

Note to myself: don't confuse:


  • IWshRuntimeLibrary.WshShell with Shell32.Shell in Microsoft Shell Controls and Automation
  • WshShell.SpecialFolders() with FileSystemObject.GetSpecialFolders() function. The latter function only returns a small number of special folders (windows, system and temporary).

No comments:

Post a Comment