Excel-VBA-Collection
133 строки · 3.9 Кб
1Attribute VB_Name = "MiscFiles"
2Option Explicit
3Option Compare Text
4Option Base 1
5DefLng A-Z
6
7Const DefaultMask = "Все файлы (*.*),*.*"
8
9Public Function BrowseForFile(ByRef File As String, Optional Mask As String = vbNullString, _
10Optional Capt As String = "файл", Optional Force As Boolean = False) As Boolean
11Dim f As Variant, s1 As String, s2 As String, Home As String
12s1 = FileNameExt(File)
13If Not Force Then
14If IsFile(File) Then
15BrowseForFile = True
16Exit Function
17End If
18End If
19On Error Resume Next
20Home = CurDir
21If Len(Mask) = 0 Then
22Mask = DefaultMask
23Else
24Mask = Mask & "," & DefaultMask
25End If
26With Application
27'set by default
28.DefaultFilePath = Application.Path
29ChDrive .DefaultFilePath
30ChDir .DefaultFilePath
31
32.DefaultFilePath = FilePath(File) 'Some problems on some computers
33ChDrive .DefaultFilePath
34ChDir .DefaultFilePath
35End With
36Do
37f = Application.GetOpenFilename(Mask, 1, "Укажите " & Capt)
38If f <> False Then 'don't change this!
39File = CStr(f)
40Else
41BrowseForFile = False
42ChDrive Home
43ChDir Home
44Exit Function
45End If
46s2 = FileNameExt(File)
47
48If Not Force Then Exit Do
49If UCase(s1) = UCase(s2) Then Exit Do
50If YesNoBox("ВНИМАНИЕ! Возможно, Вы указали не тот файл,\n" & _
51"который ждет от Вас программа:\n\n%s\n(вместо ожидаемого %s)\n\n" & _
52"Все равно использовать этот файл?", File, s1) Then Exit Do
53Loop
54BrowseForFile = IsFile(File)
55ChDrive Home
56ChDir Home
57End Function
58
59Public Function BrowseForFiles(ByRef Files As Variant, Optional Mask As String = vbNullString, _
60Optional Capt As String = "файл(ы)", Optional FilterIndex As Long = 1) As Boolean
61Dim f As Variant, Home As String
62On Error Resume Next
63Home = CurDir
64If Len(Mask) = 0 Then
65Mask = DefaultMask
66Else
67Mask = Mask & "," & DefaultMask
68End If
69With Application
70.DefaultFilePath = FilePath(CStr(Files)) 'Some problems on some computers
71ChDrive .DefaultFilePath
72ChDir .DefaultFilePath
73f = .GetOpenFilename(Mask, FilterIndex, "Укажите " & Capt, , True)
74End With
75If f <> False Then 'don't change this!
76BrowseForFiles = True
77Files = f
78Else
79BrowseForFiles = False
80End If
81ChDrive Home
82ChDir Home
83End Function
84
85Public Function BrowseForSave(ByRef File As String, Optional Mask As String = vbNullString, _
86Optional Capt As String = "файл") As Boolean
87Dim f As Variant, Home As String
88BrowseForSave = False
89On Error Resume Next
90Home = CurDir
91If Len(Mask) = 0 Then
92Mask = DefaultMask
93Else
94Mask = Mask & "," & DefaultMask
95End If
96With Application
97.DefaultFilePath = FilePath(File) 'Some problems on some computers
98ChDrive .DefaultFilePath
99ChDir .DefaultFilePath
100f = .GetSaveAsFilename(File, Mask, 1, "Укажите " & Capt)
101End With
102ChDrive Home
103ChDir Home
104If f = False Then Exit Function
105File = CStr(f)
106BrowseForSave = True
107End Function
108
109Public Function IsFile(File As String) As Boolean
110IsFile = False
111On Error GoTo ErrDir
112IsFile = GetAttr(File)
113IsFile = Not IsDir(File)
114ErrDir:
115End Function
116
117Public Function IsFile1(RunCmd As String) As Boolean
118Dim File As String, Arr As Variant
119IsFile1 = False
120On Error GoTo ErrDir
121Arr = StrToArr(RunCmd)
122File = Arr(1)
123IsFile1 = GetAttr(File)
124IsFile1 = Not IsDir(File)
125ErrDir:
126End Function
127
128Public Function IsDir(File As String) As Boolean
129IsDir = False
130On Error GoTo ErrDir
131IsDir = GetAttr(File) And vbDirectory
132ErrDir:
133End Function
134