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
   
Catch:
    OpenDocument = False
    MsgBox "Error#:  " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function

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

Call OpenDocument("http://www.geeksengine.com")

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
        IE.Quit
        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
    
Catch:
    OpenDocument2 = False
    MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description
End Function

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

Call OpenDocument2("Firefox", "http://www.geeksengine.com")

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
   
Catch:
    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 ("http://www.geeksengine.com")

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

Happy Coding!


Copyright© GeeksEngine.com



Other Recent Articles from the MS Access category:

1.Examples of MS Access DateDiff function used in query and VBA code
2.MS Access DateDiff function
3.How to find out your computer name and username by VBA
4.Examples of MS Access DatePart function
5.MS Access DatePart function
6.Examples of MS Access DateAdd function
7.MS Access DateAdd function
8.IIF function basics - the CASE statement of MS Access
9.MS Access Date Expression
10.Solved: MS Access error "The text is too long to be edited"
11.Create MS Access Combo Box essential properties by VBA code
12.Create MS Access Combo Box essential properties manually
13.How to do text search in MS Access programmatically
14.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
15.How to easily get a list of field names in MS Access
16.How to count distinct records in MS Access
17.How to do transaction based processing in MS Access
18.How to use ADOX to create unique composite index - the VBA approach
19.How to do cross-table update queries in MS Access - the right way
20.Three efficient ways to get the number of records by using VBA
21.How to create a composite unique index (not as a primary key) in MS Access
22.Use VBA to get the correct number of records in a Recordset object
23.Disable Access Prompt when a record is changed, table deleted, or action queries run
24.How to hide and unhide a MS Access object
25.How to return multiple values from a VBA function (Part 3)
26.How to return multiple values from a VBA function (Part 2)
27.How to return multiple values from a VBA function (Part 1)
28.Three ways to programmatically duplicate a table in MS Access by VBA
29.Create a DLL by CSharp or VB.Net for VBA
30.How to correctly reference and call a DLL
31.How to register a C# or VB.Net DLL
32.Email address validation by Regular Expressions using VBA
33.Fix MS Access error: Query must have at least one destination field
34.How to unselect radio buttons in MS Access after it has been selected
35.How to Change Query Timeout Value for MS Access SQL Queries
36.What is Northwind Traders database

Copyright © 2024 GeeksEngine.com. 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