Access VBA – Open Excel File

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 9, 2022

Open Excel File

This function below will open an Excel file with Access VBA.

Before using the function, make sure to add a reference to Microsoft Excel Object Library by going to Tools > References and checking Microsoft Excel Object Library.

Public Function OpenExcelFile(strFilePath As String) As Boolean
    'Required: Tools > Refences: Add reference to Microsoft Excel Object Library
    
    Dim appExcel As Excel.Application
    Dim myWorkbook As Excel.Workbook

    Set appExcel = CreateObject("Excel.Application")
    Set myWorkbook = appExcel.Workbooks.Open(strFilePath)
    appExcel.Visible = True
    
    'Do Something or Just Leave Open
    
    Set appExcel = Nothing
    Set myWorkbook = Nothing

End Function

'Usage Example
Sub OpenExcelFile_Example()
    Call OpenExcelFile("C:\Users\StevePC2\Downloads\test.xlsx")
End Sub

Open Word Document Using Access VBA

First set a reference to the Word Library for the Access VBA Environment.

Note: You would do this in the VBE by going to Tools>References and ticking Microsoft Word 15.0 Object Library.

The following code will allow you to open your Word document using Access:

Dim WordDoc As Object

Set WordDoc = CreateObject("Word.Application")
WordDoc.Visible = True
WordDoc.Documents.Open "C:\Users\Taryn\Desktop\BusinessDocument.docx"
Set WordDoc = Nothing

Or you can adapt the Open Excel File function above to create a function to open a Word document from Access VBA.

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples