VBAで資料を作成しています。
フォルダをフルパスで指定をして
その中に入っているすべてのテキストファイルの
中から、特定のキーワードが入っている文字列を
抜出たいと考えています。
###発生している問題・エラーメッセージ
フォルダをフルパスで指定しその中のすべてのテキストを
指定する方法がわかりません。
エラーメッセージ
オブジェクトはプロパティまたはメソッドをサポートしていません。
###該当のソースコード
Sub SerchPath()
'
Dim i As Integer
Dim filepath As String
Dim line As Variant 'ファイルの1行毎を配列として格納する変数
Dim outpos As Long '出力セル開始位置
outpos = 10 '10行目から書き出し
filepath = Range("C4").Value '☆
Dim buf As String 'ファイルの全文字列
'ファイルを開いてbuf変数へ格納
With CreateObject("Scripting.FileSystemObject")
With .GetForder(filepath).OpenAsTextStream
buf = .ReadAll
.Close
End With
End With
'改行コードで一行毎に分割し配列変数へ代入
line = Split(buf, vbCrLf)
'一行毎に検索を実行する
Dim str As String '一時的に1行の文字列を格納
Dim startword As String 'この文字から抜き出す
Dim endword As String 'この文字まで抜き出す
Dim startpos As Long '抜き出す文字の開始位置
Dim endpos As Long '抜き出す文字の終了位置
Dim endpos2 As Long 'さらに最後のDMELを抜き出すのでその文字位置
Dim keyword As String 'この文字を含む
Dim keyword2 As String 'この文字も含む
Dim out As String
startword = Range("S1").Text '検索定義をシートから読み込む
endword = Range("V1").Text '検索定義をシートから読み込む
keyword = Range("S2").Text '検索定義をシートから読み込む
keyword2 = Range("V2").Text '検索定義をシートから読み込む
For i = 0 To UBound(line)
str = line(i)
startpos = InStr(str, startword)
endpos = InStr(str, endword)
'行に startword endword keyword 全ての文字列が含まれているか判定 If InStr(str, startword) > 0 And InStr(str, endword) > 0 And InStr(str, keyword) > 0 And InStr(str, keyword2) > 0 Then out = Mid(str, startpos, endpos - startpos) endpos2 = InStrRev(out, keyword3) out = Mid(out, 1, endpos2 - 1) 'セルB10~へ対して行の抜き出した文字列を書き出す Cells(outpos, 3).Value = out outpos = outpos + 1 '書き出し位置を下へずらす End If
Next i
'
'
End Sub
###試したこと
With .GetForder(filepath).OpenAsTextStreamで
エラーがでてしまいます。
###補足情報(言語/FW/ツール等のバージョンなど)
VBAで作成してます。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/10/24 00:50