Public Function RefreshExcelLink(ByVal strLink As String)
'**************************************************
'*
'* strLink is the name of Excel Link table
'* in Navigation Pane
'*
'**************************************************
Dim td As TableDef
Dim db As DAO.Database
Dim strFile As String
Dim strConnect As String
Dim strSource As String
Dim strPath As String
Dim strExt As String
On Error Resume Next
Set db = CurrentDb
Set td = db.TableDefs(strLink)
'* get original sourcetable and connection string
strSource = td.SourceTableName
strConnect = td.Connect
'* close the link file
Set td = Nothing
'* remove portion of connection string
strPath = Mid(strConnect, InStrRev(strConnect, "DATABASE="))
strConnect = Left(strConnect, Len(strConnect) - Len(strPath))
'* extract the path of file to link
strPath = Replace(strPath, "DATABASE=", "")
'* extract the extension portion
strExt = Mid(strPath, InStrRev(strPath, ".") + 1)
'* extract the path to link (final)
strPath = Left(strPath, InStrRev(strPath, "\"))
'* get the last modified file
strFile = LastModifiedFile(strPath, strExt)
'* delete old link file
db.TableDefs.Delete (strLink)
'* create new link file
Set td = db.CreateTableDef(strLink)
With td
.Connect = strConnect & "DATABASE=" & strFile
.SourceTableName = strSource
End With
db.TableDefs.Append td
Set td = Nothing
db.TableDefs.Refresh
Set db = Nothing
Application.RefreshDatabaseWindow
End Function
Public Function LastModifiedFile(ByVal strPath As String, Optional ByVal strExt As String = "") As Variant
Dim oLastFile As Object
Dim oFile As Object
Dim oFS As Object
On Error GoTo ExitFunction
strPath = Replace(strPath & "\", "\\", "\")
strExt = LCase(strExt)
Set oFS = CreateObject("Scripting.FileSystemObject")
For Each oFile In oFS.GetFolder(strPath).Files
If strExt = "" Or strExt = LCase(oFS.GetExtensionName(oFile.Name)) Then
If oLastFile Is Nothing Then
Set oLastFile = oFile
Else
If oLastFile.DateLastModified < oFile.DateLastModified Then
Set oLastFile = oFile
End If
End If
End If
Next
LastModifiedFile = strPath & oLastFile.Name
ExitFunction:
Set oLastFile = Nothing
Set oFile = Nothing
Set oFS = Nothing
End Function