2016-11-13 26 views
0

他のアプリケーションがdata.txtファイルにデータを書き込んでいて、VBAがそれを読み込もうとすると、Excel VBA - 例外:許可が拒否されたエラー70

これらの例外を無視する方法がありますか、ファイルが自由にアクセスできるようになるまで待ってからコードを実行し続けるのが良いですか?

fileName = "C:\TEXT\data.txt" 
fileNo = FreeFile 'Get first free file number 

Open fileName For Input As #fileNo 
Do While Not EOF(fileNo) 
    Line Input #fileNo, textRow 
    jsonText = textData & textRow 
Loop 
Close #fileNo 
+0

ファイルをロックする他のプロセスは短命ですか?あなたはどれくらい待っていますか? –

+0

お待ちください。あなたはプログラマーです。成功するまでループします。 –

+0

@TimWilliams短命です。それは数ミリリットルの長さでなければならない。 – Totallama

答えて

0

私はこれを試してみましたが、私はそれが100%の信頼性があるかどうかわからないです:

Dim continue As Boolean 

fileName = "C:\TEXT\data.txt" 
fileNo = FreeFile 'Get first free file number 

continue = True 

Do While continue 
On Error Resume Next 
Open fileName For Input As #fileNo 
If Err Then 
continue = True 
Application.Wait (Now + 0.000001) 
Else 
Do While Not EOF(fileNo) 
    Line Input #fileNo, textRow 
    jsonText = textData & textRow 
Loop 
Close #fileNo 
continue = False 
End If 
Loop 
0

は、ここで私が何を意味するかの例です。

fileName = "C:\TEXT\data.txt" 
fileNo = FreeFile 'Get first free file number 

On Error Resume Next 
Do 
    Open fileName For Input As #fileNo 
    If err.number = 0 then 
     Exit Do 
    Else 
     Err.clear 
    End If 
Loop 
On Error Goto 0 

Do While Not EOF(fileNo) 
    Line Input #fileNo, textRow 
    jsonText = textData & textRow 
Loop 
Close #fileNo 
+0

Tnxです。しかし、私はすでに私のことを思い付いたので、うまくいくように見えるので、私はそれに固執すると思います。 – Totallama

関連する問題