Skip to content

Commit e51a066

Browse files
committed
update CodeLib modules
1 parent dc825c8 commit e51a066

File tree

6 files changed

+394
-56
lines changed

6 files changed

+394
-56
lines changed

source/modules/FileTools.bas

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Private Declare PtrSafe Function API_GetTempFilename Lib "kernel32" Alias "GetTe
5858
ByVal wUnique As Long, _
5959
ByVal lpTempFileName As String) As Long
6060

61-
Private Declare PtrSafe Function API_ShellExecuteA Lib "shell32.dll" ( _
61+
Private Declare PtrSafe Function API_ShellExecuteA Lib "shell32.dll" Alias "ShellExecuteA" ( _
6262
ByVal Hwnd As LongPtr, _
6363
ByVal lOperation As String, _
6464
ByVal lpFile As String, _
@@ -81,7 +81,7 @@ Private Declare Function API_GetTempFilename Lib "kernel32" Alias "GetTempFileNa
8181
ByVal wUnique As Long, _
8282
ByVal lpTempFileName As String) As Long
8383

84-
Private Declare Function API_ShellExecuteA Lib "shell32.dll" ( _
84+
Private Declare Function API_ShellExecuteA Lib "shell32.dll" Alias "ShellExecuteA" ( _
8585
ByVal Hwnd As Long, _
8686
ByVal lOperation As String, _
8787
ByVal lpFile As String, _
@@ -848,26 +848,60 @@ End Function
848848
' Boolean
849849
'
850850
'---------------------------------------------------------------------------------------
851-
Public Function OpenFile(ByVal FilePath As String, Optional ByVal ReadOnlyMode As Boolean = False) As Boolean
851+
Public Function OpenFile(ByVal FilePath As String, Optional ByVal ReadOnlyMode As Boolean = False, _
852+
Optional ByVal DefaultFileFolderIfFileNameOnly As String = vbNullString) As Boolean
852853

853854
Const FileNotFoundErrorTextTemplate As String = "File '{FilePath}' not found."
854855
Dim FileNotFoundErrorText As String
856+
Dim FilePath2Open As String
855857

856-
If Len(VBA.Dir(FilePath)) = 0 Then
858+
If Len(DefaultFileFolderIfFileNameOnly) > 0 Then
859+
FilePath2Open = BuildFullFileName(FilePath, DefaultFileFolderIfFileNameOnly)
860+
Else
861+
FilePath2Open = FilePath
862+
End If
863+
864+
If Len(VBA.Dir(FilePath2Open)) = 0 Then
857865

858866
#If USELOCALIZATION = 1 Then
859-
FileNotFoundErrorText = Replace(L10n.Text(FileNotFoundErrorTextTemplate), "{FilePath}", FilePath)
867+
FileNotFoundErrorText = Replace(L10n.Text(FileNotFoundErrorTextTemplate), "{FilePath}", FilePath2Open)
860868
#Else
861-
FileNotFoundErrorText = Replace(FileNotFoundErrorTextTemplate, "{FilePath}", FilePath)
869+
FileNotFoundErrorText = Replace(FileNotFoundErrorTextTemplate, "{FilePath}", FilePath2Open)
862870
#End If
863871
Err.Raise VbaErrNo_FileNotFound, "FileTools.OpenFile", FileNotFoundErrorText
864872
Exit Function
865873
End If
866874

867-
OpenFile = ShellExecute(FilePath, "open")
875+
OpenFile = ShellExecute(FilePath2Open, "open")
868876

869877
End Function
870878

879+
Public Function BuildFullFileName(ByVal FileName As String, ByVal DefaultFileFolderIfFileNameOnly As String) As String
880+
881+
If Len(DefaultFileFolderIfFileNameOnly) = 0 Then
882+
BuildFullFileName = FileName
883+
Exit Function
884+
End If
885+
886+
If Left(FileName, 2) = "\\" Then 'Win-Share
887+
BuildFullFileName = FileName
888+
Exit Function
889+
End If
890+
891+
If Mid(FileName, 2, 1) = ":" Then 'Laufwerksbuchstabe
892+
BuildFullFileName = FileName
893+
Exit Function
894+
End If
895+
896+
If Left(FileName, 1) <> "\" And Right(DefaultFileFolderIfFileNameOnly, 1) <> "\" Then
897+
DefaultFileFolderIfFileNameOnly = DefaultFileFolderIfFileNameOnly & "\"
898+
End If
899+
900+
BuildFullFileName = DefaultFileFolderIfFileNameOnly & FileName
901+
902+
End Function
903+
904+
871905
'---------------------------------------------------------------------------------------
872906
' Function: OpenFilePath
873907
'---------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)