Custom Search

How to open a document (local/network file or web page) from MS Access

When developing Access database applications, you may want to open a document (a file on your local or network drive, or a web page) by the user or automatically opening the document in a background process coded in VBA. For example, after processing customer orders, Access opens web browser that displays sales and orders information for this month in HTML web page format.

There are three ways to open a document by Access - using FollowHyperlink method, Shell function, or Navigate method.

1. Using FollowHyperlink method

The FollowHyperlink Method of Access Application object opens a document or Web page specified by a hyperlink address.

For a web page, it's always opened by the default web browser of your computer. If you want the web page to be opened in a web browser other than the default one, use the second method that uses Shell function.

For a file on your local computer or network, it'll be opened by the default program of your PC.

Function OpenDocument(ByVal strURLLink As String) As Boolean    
    On Error Goto Catch
    '' This opens the link in the default program or web browser of your computer.
    Application.FollowHyperlink (strURLLink)
    OpenDocument = True
    Exit Function
    OpenDocument = False
    MsgBox "Error#:  " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function

To test this function, use the code below. It opens website in your default web browser.

Call OpenDocument("")

2. Using Shell function

Shell function of MS Access runs an executable program so if we specify the EXE file of a web browser (e.g. Firefox.exe), the web page can be opened by that browser, or if the document is a file on your local computer or network, the web browser will prompt you to choose a program to open the file.

'' Open a local file or a web page in the specified web browser.

'' Optional argument intFileWindowStatus has these values:

'' vbHide 0
'' vbMaximizedFocus 3
'' vbMinimizedFocus 2
'' vbMinimizedNoFocus 6
'' vbNormalFocus 1
'' vbNormalNoFocus 4
Function OpenDocument2(ByVal strWebBrowser As String, _
                          ByVal strURLOrFile As String, _
                          Optional intFileWindowStatus As Integer = 3) As Boolean

    On Error Goto Catch

    Dim IE As Object
    Dim strBrowserEXEPath As String

    Dim strShellPath As String
    If strWebBrowser = "IE" Then
        Set IE = CreateObject("InternetExplorer.Application")
        strBrowserEXEPath = IE.FullName
        Set IE = Nothing

    ElseIf strWebBrowser = "Firefox" Then
        strBrowserEXEPath = "C:\Program Files\Mozilla Firefox\firefox.exe"
    End If
    '' Must put the file path and name within double quotes so that
    '' it's treated as a single entity.
    strShellPath = strBrowserEXEPath & " """ & Trim(strURLOrFile) & """"
    '' Open the doc
    Call Shell(strShellPath, intFileWindowStatus)
    OpenDocument2 = True

    Exit Function
    OpenDocument2 = False
    MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description
End Function

To test this function, use the code below. It opens website in Firefox.

Call OpenDocument2("Firefox", "")

3. Using the Navigate method of an Internet Explorer instance

The third method opens a document in Internet Explorer only. It creates an instance of Internet Explorer, then uses the Navigate method to open a web page or file.

You can open a URL or a file by passing the appropriate path to the Navigate method.

Setting the Visible property to False will hide Internet Explorer window. This can be useful when opening a document that can't be opened directly by Internet Explorer, such as opening a Microsoft Word document, because an IE window is not opened at all. Regardless of what you specify in the Visible property, if IE can't open the document, you'll be prompted to open or save the document anyway. See screenshot below.

For more about the CreateObject Method, refer to Microsoft page here.

Here is the third method.

Function OpenDocument3(ByVal strLink As String) As Boolean
    On Error Goto Catch
    Dim objBrowser As Object

    Set objBrowser = CreateObject("InternetExplorer.application")
    objBrowser.Visible = True
    objBrowser.Navigate strLink

    Exit Function
    OpenDocument3 = False
    MsgBox "Error#:  " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function

To test this function, use the code below.

'' Open a web page
OpenDocument3 ("")

'' Open a local Word document
OpenDocument3 ("D:\Specs.docx")

Happy Coding!


Other Recent Articles from the MS Access category:

1.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
2.How to easily get a list of field names in MS Access
3.How to count distinct records in MS Access
4.How to do transaction based processing in MS Access
5.How to use ADOX to create unique composite index - the VBA approach
6.How to do cross-table update queries in MS Access - the right way
7.Three efficient ways to get the number of records by using VBA
8.How to create a composite unique index (not as a primary key) in MS Access
9.Use VBA to get the correct number of records in a Recordset object
10.Disable Access Prompt when a record is changed, table deleted, or action queries run
11.How to hide and unhide a MS Access object
12.How to return multiple values from a VBA function (Part 3)
13.How to return multiple values from a VBA function (Part 2)
14.How to return multiple values from a VBA function (Part 1)
15.Three ways to programmatically duplicate a table in MS Access by VBA
16.Create a DLL by CSharp or VB.Net for VBA
17.How to correctly reference and call a DLL
18.How to register a C# or VB.Net DLL
19.Email address validation by Regular Expressions using VBA
20.Fix MS Access error: Query must have at least one destination field
21.How to unselect radio buttons in MS Access after it has been selected
22.How to Change Query Timeout Value for MS Access SQL Queries
23.What is Northwind Traders database

Copyright © 2017 All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

Home | Feedback | Terms of Use | Privacy Policy