Пока MSComDlg содержалась в Windows по умолчанию, было примерно так, как в GetSourceName
Но теперь она перестала поставляться. Значит или добавлять dll самому, или искать другой путь. Например Inputbox или свою форму. Или вызывать диалог непосредственно через WinAPI без ActiveX.
Код:
'Function - "Select the source file and create DataRecordSets"
Public Sub ConnectToExternalData()
On Error Resume Next
fSource = GetSourceName '==> Get file name
If Err.Number > 0 Then
fSource = SetExcelPath 'Used if MSComDlg is missing
End If
On Error GoTo 0
If fSource = "" Then Exit Sub
SetNewDRS fSource '==> Create DRS
End Sub
Private Function GetSourceName() As String
Dim CDLG As Object
Set CDLG = CreateObject("MSComDlg.CommonDialog")
With CDLG
.DialogTitle = "Get source file"
.Filter = "Excel files (*.xlsx)|*.xlsx"
.FileName = Mid(ActiveDocument.Path, 1, Len(ActiveDocument.Path) - 1)
.ShowOpen
s = .FileName
End With
res = Dir(s) 'If file exists
If res = "" Then
MsgBox "File " & s & " is missing"
GetSourceName = ""
Else
GetSourceName = s
End If
Set CDLG = Nothing
End Function