回答編集履歴

1

追記

2017/10/23 08:56

投稿

jawa
jawa

スコア3013

test CHANGED
@@ -51,3 +51,151 @@
51
51
 
52
52
 
53
53
  参考になれば幸いです。
54
+
55
+
56
+
57
+
58
+
59
+ (追記)
60
+
61
+ ---
62
+
63
+ ```
64
+
65
+ 'メイン処理
66
+
67
+ Private Sub Main()
68
+
69
+
70
+
71
+ Const F_PATH = "c:\temp\testFormat.txt"
72
+
73
+
74
+
75
+ Dim shtTags As Worksheet
76
+
77
+ Set shtTags = Sheets("固定値シート")
78
+
79
+
80
+
81
+ Dim strSQL As String
82
+
83
+ '①テンプレート取得
84
+
85
+ strSQL = GetTemplate(F_PATH)
86
+
87
+
88
+
89
+ '②タグ置換
90
+
91
+ strSQL = ReplaceTag(strSQL, "INSTANCENAME", shtTags.Cells(1, "B").Value)
92
+
93
+ strSQL = ReplaceTag(strSQL, "PACKAGENAME", shtTags.Cells(2, "B").Value)
94
+
95
+ 'strSQL = ReplaceTag(strSQL, "tableName", "TBL_A")
96
+
97
+
98
+
99
+ '③作成したSQLをテキスト保存(未実装)
100
+
101
+ Call SaveTemplate(F_PATH, strSQL)
102
+
103
+
104
+
105
+ End Sub
106
+
107
+
108
+
109
+ '①テンプレート取得
110
+
111
+ Private Function GetTemplate(ByVal vsFilename As String) As String
112
+
113
+ Dim f_num As Integer ' ファイル番号
114
+
115
+ Dim rcd As String ' テキストファイルの1行分文字列(1レコード)
116
+
117
+ Dim strRet As String
118
+
119
+
120
+
121
+ f_num = FreeFile
122
+
123
+ Open vsFilename For Input As f_num
124
+
125
+
126
+
127
+ Do Until EOF(f_num)
128
+
129
+ Line Input #f_num, rcd
130
+
131
+ strRet = strRet + vbCrLf & rcd
132
+
133
+ 'Debug.Print rcd
134
+
135
+ Loop
136
+
137
+
138
+
139
+ 'Debug.Print strSQL
140
+
141
+ Close f_num
142
+
143
+
144
+
145
+ '取得した内容を戻り値として返す
146
+
147
+ GetTemplate = strRet
148
+
149
+ End Function
150
+
151
+ '①テンプレート取得(一気に読むバージョン)
152
+
153
+ Private Function GetTemplate2(ByVal vsFilename As String) As String
154
+
155
+ Dim fso As Object
156
+
157
+ Dim strRet As String
158
+
159
+ Set fso = CreateObject("Scripting.FileSystemObject")
160
+
161
+ With fso.GetFile(vsFilename).OpenAsTextStream
162
+
163
+ strRet = .ReadAll
164
+
165
+ .Close
166
+
167
+ End With
168
+
169
+ GetTemplate2 = strRet
170
+
171
+ End Function
172
+
173
+
174
+
175
+ '②タグ文字列置換
176
+
177
+ Private Function ReplaceTag(ByVal vsBASE As String, ByVal vsTAG As String, ByVal vsVALUE As String) As String
178
+
179
+ Dim strRet As String
180
+
181
+ 'タグを置換する
182
+
183
+ strRet = Replace(vsBASE, "{" & vsTAG & "}", vsVALUE)
184
+
185
+ '置換した内容を戻り値として返す
186
+
187
+ ReplaceTag = strRet
188
+
189
+ End Function
190
+
191
+
192
+
193
+ '③ファイル保存(未実装)
194
+
195
+ Private Sub SaveTemplate(ByVal vsFilename As String, vsText As String)
196
+
197
+ MsgBox vsText
198
+
199
+ End Sub
200
+
201
+ ```