Opening A File With MS Office Object Library

For this one you will need to add a reference to the “MS office object library”

Then you can write something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Sub OpenFile()
   strFileName = GetFile(strStart, "Select Your File")

   'open your file, import it, or here we are just displaying the name
   Msgbox strFileName
End Sub

Private Function GetFile(start_here, title_bar As String) As String
    Dim intChoice As Integer
    Dim strPath As String
   
    'needs reference to the MS office object library
   
    'only allow the user to select one file
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .InitialFileName = start_here
        .Title = title_bar
    End With
   
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        strPath = Application.FileDialog( _
            msoFileDialogOpen).SelectedItems(1)
        'MsgBox strPath
       
        get_file = strPath
    End If
End Function

This is pretty straight forward, or you can also use my Shell method

Offsite Related Information:

“Go Fund Me” Page


($5 suggested amount)

(…10% of your gift amount will go to charity)

Free! Subscribe To Our YouTube Channel!

Free MS Access VBA Programming Course

Facebooktwitterredditpinterestlinkedinmailby feather
Tags: , , ,