We help IT Professionals succeed at work.

Need some vb6 code to set the homepage in firefox 2.0

I need to on a command1_click set the home page on firefox to "www.myhomepage.com" the code need to be in visual basic 6 .
Watch Question

Shanmuga SundaramDirector of Software Engineering

usually the homepage can be set by writing in registry


There are plenty of codes available in vbaccelerator.com, a1vbcode.com,programmersheaven.com to handle registry. Please check and use it to accompolish the task


I thought firefox don't save the homepage to the registry


will that set all webbrowsers ie,firefox , ect ?
See the following line in prefs.js file.

user_pref("browser.startup.homepage", "http://www.google.co.uk/ig");

located in the application data folder, for example:

C:\Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\random.default\prefs.js

Warning at the top of the file that edits made while firefox is running will be overwritten when it exits.

Complete code.  Requires reference to Microsoft VBScript Regular Expressions 5.5
Private Sub SetFirefoxHomepage(URL As String)
    Dim strPath As String, strProfile As String
    Dim strContent As String, strReplace As String
    Dim regex As RegExp
    strPath = "C:" & Environ("HOMEPATH")
    strPath = strPath & "\Application Data\Mozilla\Firefox\Profiles\"
    strProfile = Dir(strPath & "*.default", vbDirectory)
    If Len(strProfile) Then
        Debug.Print strProfile
        strPath = strPath & strProfile & "\prefs.js"
        strContent = fGetFileContents(strPath)
        Set regex = New RegExp
        regex.Pattern = "user_pref\(""browser.startup.homepage"",\s""(.*)""\);"
        strReplace = "user_pref(""browser.startup.homepage"", """ & URL & """);"
        Debug.Print strReplace
        strContent = regex.Replace(strContent, strReplace)
        sPutStringToFile strContent, strPath
    End If
End Sub
Public Function fGetFileContents(strPath As String) As String
    Dim hFile As Integer
    Dim strFileContent As String
    If Len(Dir(strPath)) = 0 Then Exit Function
    On Error GoTo errGetFile
    hFile = FreeFile
    Open strPath For Binary As #hFile
    strFileContent = Space(LOF(hFile))
    Get #hFile, , strFileContent
    Close #hFile
    fGetFileContents = strFileContent
    Exit Function
    MsgBox Err.Description, vbCritical, "GetFileContents"
End Function
Public Sub sPutStringToFile(strContent As String, strPath As String)
    Dim hFile As Integer
    'If file exists delete it.
    On Error Resume Next
    Kill strPath
    On Error GoTo errPutString
    'Write file
    hFile = FreeFile
    Open strPath For Binary As #hFile
    Put #hFile, , strContent
    Close #hFile
    Exit Sub
    MsgBox Err.Description, vbCritical, "PutStringToFile"
End Sub

Open in new window


this is the only thing in the file for me "# Mozilla User Preferences

/* Do not edit this file.
 * If you make changes to this file while the browser is running,
 * the changes will be overwritten when the browser exits.
 * To make a manual change to preferences, you can visit the URL about:config
 * For more information, see http://www.mozilla.org/unix/customizing.html#prefs


how would you call it ?
SetFirefoxHomepage "http://yahoo.com"

If you don't have any preferences set (from the empty preference file above) then it probably won't work.  You would have to set up some defaults.  You could write them from VB or copy a file programmatically into the profile folder.
>how would you call it ?
SetFirefoxHomepage "http://yahoo.com"<

Yep.  When there are defaults already in the file.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.