Return to VBA Code Examples

Access VBA – Open Excel File

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.