teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

<code>を使った方法へ記述を入れ替え

2020/05/19 00:53

投稿

tosi
tosi

スコア553

answer CHANGED
@@ -3,45 +3,40 @@
3
3
  最下部のstrDefFolderNameへ存在するネットワークフォルダ名へ入れ替えて
4
4
  runOpenFileDialog_Excelを動かして見て下さい。
5
5
  私にはこれしかやり方が分かりません。
6
- '-----------------------------------------------
7
- (ソースコード)
6
+ ```ここに言語を入力
8
-
9
7
  Option Explicit
10
- '***********************************************
8
+ '*******************************************
11
9
  ' Private Work Area
12
- '***********************************************
10
+ '*******************************************
13
11
  Private strFilter As String
14
12
  Private strTitle As String
15
13
  Private strDefFolderName As String
16
- '***********************************************
14
+ '*******************************************
17
15
  ' API Area
18
- '***********************************************
16
+ '*******************************************
19
- 'Get Open File
20
- Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
17
+ Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
21
- (lpofn As OPENFILENAME) As Long
22
18
  Private Type OPENFILENAME
23
- lStructSize As Long '構造体のサイズ
19
+ lStructSize As Long '構造体のサイズ
24
- hwndOwner As Long 'ダイアログボックスの親ウィンドウハンドル
20
+ hwndOwner As Long 'ダイアログボックスの親ウィンドウハンドル
25
- hInstance As Long 'テンプレートリソースを持つモジュールのインスタンスハンドル(不用のとき0)
21
+ hInstance As Long 'テンプレートリソースを持つモジュールのインスタンスハンドル(不用のとき0)
26
- lpstrFilter As String 'フィルタ(Visual Basicのファイルパターンのこと)
22
+ lpstrFilter As String 'フィルタ(Visual Basicのファイルパターンのこと)
27
- lpstrCustomFilter As String 'カスタムフィルタ
23
+ lpstrCustomFilter As String 'カスタムフィルタ
28
- nMaxCustomFilter As Long '同、バイト数
24
+ nMaxCustomFilter As Long '同、バイト数
29
- nFilterIndex As Long 'ダイアログに優先的に表示するフィルタのインデックス
25
+ nFilterIndex As Long 'ダイアログに優先的に表示するフィルタのインデックス
30
- lpstrFile As String '(戻り値)フルパス名を受け取るバッファ
26
+ lpstrFile As String '(戻り値)フルパス名を受け取るバッファ
31
- nMaxFile As Long '同、バイト数
27
+ nMaxFile As Long '同、バイト数
32
- lpstrFileTitle As String '(戻り値)ファイル名を受け取るバッファ
28
+ lpstrFileTitle As String '(戻り値)ファイル名を受け取るバッファ
33
- nMaxFileTitle As Long '同、バイト数
29
+ nMaxFileTitle As Long '同、バイト数
34
- lpstrInitialDir As String '初期のディレクトリ名
30
+ lpstrInitialDir As String '初期のディレクトリ名
35
- lpstrTitle As String 'ダイアログのキャプション
31
+ lpstrTitle As String 'ダイアログのキャプション
36
- flags As Long '動作を指定
32
+ flags As Long '動作を指定
37
- nFileOffset As Integer 'フルパス中のファイル名までのオフセット
33
+ nFileOffset As Integer 'フルパス中のファイル名までのオフセット
38
- nFileExtension As Integer '同、拡張子までのオフセット
34
+ nFileExtension As Integer '同、拡張子までのオフセット
39
- lpstrdefext As String 'デフォルトの拡張子
35
+ lpstrdefext As String 'デフォルトの拡張子
40
- lCustData As Long 'フックプロシージャに渡すデータ
36
+ lCustData As Long 'フックプロシージャに渡すデータ
41
- lpfnHook As Long 'フックプロシージャへのポインタ
37
+ lpfnHook As Long 'フックプロシージャへのポインタ
42
- lpTemplateName As String 'テンプレートリソース名
38
+ lpTemplateName As String 'テンプレートリソース名
43
- End Type
39
+ End Type
44
-
45
40
  'uFlagsの定数
46
41
  Private Const OFN_ALLOWMULTISELECT = &H200 '複数ファイルを選択可能にする
47
42
  Private Const OFN_CREATEPROMPT = &H2000 '指定のファイル名がないとき、ファイルを作成するかどうかを問い合わせるダイアログを表示する
@@ -65,90 +60,77 @@
65
60
  Private Const OFN_NOCHANGEDIR = &H8 'ダイアログ終了後、元のディレクトリに戻る
66
61
  Private Const OFN_NOLONGNAMES = &H40000 '旧スタイルのダイアログのとき、ショートファイル名を使用可能にする(エクスプローラ型のときは常にロングファイル名が使える)
67
62
  Private Const OFN_NONETWORKBUTTON = &H20000 'ネットワークボタンを非表示・無効にする
68
- Private Const OFN_SHAREAWARE = &H4000 'ファイルを開いたときにネットワーク共有違反のためエラーが発生してもエラーを無視する
63
+ Private Const OFN_SHAREAWARE = &H4000 'ファイルを開いたときにネットワーク共有違反のためエラーが発生してもエラーを無視する
69
-
70
64
  Private lpofn As OPENFILENAME
71
- '
65
+ '
66
+ '**************************************************************
67
+ ' @(f)
68
+ ' Function : OpenFileDialog
69
+ ' Return : True:Nomal False:Abnomal
70
+ ' Argument :
71
+ ' Description :
72
+ ' Note :
73
+ '**************************************************************
74
+ Private Function OpenFileDialog() As Boolean
75
+ Dim fs As Object
76
+ Dim MyFolder As Object
77
+ Dim MyPath As String
78
+ Dim strFileName As String
79
+ Dim blnErr As Boolean
80
+ Dim lpofn As OPENFILENAME
81
+ Dim rc As Variant
82
+ Dim a As Long
83
+ Set fs = CreateObject("Scripting.FileSystemObject")
84
+ If fs.FolderExists(strDefFolderName) = False Then
85
+ MsgBox "Error Exist:" & strDefFolderName
86
+ Exit Function
87
+ Else
88
+ MyPath = strDefFolderName
89
+ End If
90
+ With lpofn
91
+ '
92
+ .flags = OFN_PATHMUSTEXIST Or OFN_HIDEREADONLY
93
+ .lStructSize = Len(lpofn)
94
+ '.hwndOwner = Application.hWndAccessApp
95
+ .lpstrFileTitle = String(512, Chr(0))
96
+ .nMaxFileTitle = 512
97
+ .lpstrFile = String(512, Chr(0))
98
+ .nMaxFile = 512
99
+ .lpstrTitle = "Select Variable Definition File"
100
+ .lpstrFilter = strFilter
101
+ .lpstrInitialDir = MyPath
102
+ .nFilterIndex = 1
103
+ .lpstrFile = String(512, Chr(0))
104
+ '
105
+ rc = GetOpenFileName(lpofn)
106
+ If rc > 0 Then
107
+ a = InStr(.lpstrFile, Chr(0))
108
+ strFileName = Mid(.lpstrFile, 1, a - 1)
109
+ blnErr = False
110
+ Else
111
+ 'No Select
112
+ Set fs = Nothing
113
+ Exit Function
114
+ End If
115
+ '
116
+ End With
117
+ Set fs = Nothing
72
118
 
73
- '******************************************************************
119
+ End Function
74
120
 
75
- ' @(f)
121
+ Public Function runOpenFileDialog_Excel() As Boolean
76
122
 
123
+ '-------------------------------------------
124
+ 'ネットワークフォルダ名を指定
125
+ strDefFolderName = "\192.168.3.4\test"
126
+ '-------------------------------------------
127
+ strFilter = ""
128
+ strFilter = strFilter & "Excel File(*.xls)" + Chr(0) + "*.xls" + Chr(0) + ""
129
+ strFilter = strFilter & "Excel File(*.xlsx)" + Chr(0) + "*.xlsx" + Chr(0) + ""
130
+ strFilter = strFilter & "All File(*.*)" + Chr(0) + "*.*"
77
- ' Function : OpenFileDialog
131
+ If OpenFileDialog() = False Then
132
+ Exit Function
133
+ End If
78
134
 
79
- ' Return : True:Nomal False:Abnomal
80
-
81
- ' Argument :
82
-
83
- ' Description :
84
-
85
- ' Note :
86
-
87
- '******************************************************************
88
- Private Function OpenFileDialog() As Boolean
89
-
90
- Dim fs As Object
91
- Dim MyFolder As Object
92
- Dim MyPath As String
93
- Dim strFileName As String
94
- Dim blnErr As Boolean
95
-
96
- Dim lpofn As OPENFILENAME
97
- Dim rc As Variant
98
- Dim a As Long
99
-
100
- Set fs = CreateObject("Scripting.FileSystemObject")
101
- If fs.FolderExists(strDefFolderName) = False Then
102
- MsgBox "Error Exist:" & strDefFolderName
103
- Exit Function
104
- Else
105
- MyPath = strDefFolderName
106
- End If
107
-
108
- With lpofn
109
- '
110
- .flags = OFN_PATHMUSTEXIST Or OFN_HIDEREADONLY
111
- .lStructSize = Len(lpofn)
112
- '.hwndOwner = Application.hWndAccessApp
113
- .lpstrFileTitle = String(512, Chr(0))
114
- .nMaxFileTitle = 512
115
- .lpstrFile = String(512, Chr(0))
116
- .nMaxFile = 512
117
- .lpstrTitle = "Select Variable Definition File"
118
-
119
- .lpstrFilter = strFilter
120
- .lpstrInitialDir = MyPath
121
- .nFilterIndex = 1
122
- .lpstrFile = String(512, Chr(0))
123
- '
124
- rc = GetOpenFileName(lpofn)
125
- If rc > 0 Then
126
- a = InStr(.lpstrFile, Chr(0))
127
- strFileName = Mid(.lpstrFile, 1, a - 1)
128
- blnErr = False
129
- Else
130
- 'No Select
131
- Set fs = Nothing
132
- Exit Function
133
- End If
134
- '
135
- End With
136
-
137
- Set fs = Nothing
138
-
139
135
  End Function
140
-
141
- Public Function runOpenFileDialog_Excel() As Boolean
142
- '-------------------------------------------
143
- 'ネットワークフォルダ名を指定
144
- strDefFolderName = "\172.16.5.100\test\test"
145
- '-------------------------------------------
146
- strFilter = ""
147
- strFilter = strFilter & "Excel File(*.xls)" + Chr(0) + "*.xls" + Chr(0) + ""
148
- strFilter = strFilter & "Excel File(*.xlsx)" + Chr(0) + "*.xlsx" + Chr(0) + ""
149
- strFilter = strFilter & "All File(*.*)" + Chr(0) + "*.*"
150
- If OpenFileDialog() = False Then
151
- Exit Function
152
- End If
136
+ ```
153
- End Function
154
- '-----------------------------------------------