Checking if file open.txt then close

mtheriault

Board Regular
Joined
Sep 16, 2008
Messages
161
Hello again

Many thanks to all of you who helped me for the last few days.

I need to check if a .txt file is open. If yes, need to close it.

How can i do it

Martin:eeek:
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Quick and dirty solution:
Code:
Sub a()

On Error Resume Next
Workbooks("myfile.txt").Close savechanges:=False
On error goto 0

End Sub
 
Upvote 0
Thanks
JubJab, i will try that

Westconn1, The file is opened, outside of Excel. It contained an OCR ( optical characters reading) result from another software.

Martin
 
Upvote 0
Greetings,

I agree with westconn. As you are running your code in Excel, you can set a reference to Word and use early-binding or create Word as an object and use late-binding, but I think that using Word's Tasks should work.

Let's say the offending .txt file is named 'TimeKeeper.txt', then using the filename and Like could be similar to:

Code:
Sub FindTextFileRunning()
Dim tsk As Task
    For Each tsk In Tasks
        If tsk.Name Like "TimeKeeper.txt*" Then
            tsk.Close
            Exit Sub
        End If
    Next
End Sub

Please note that the above is code in Word - as mentioned, use CreateObject etc.

Hope this helps,

Mark
 
Upvote 0
don't forget to quit word when you finish and destroy reference to objects


Greetings westconn, and nice to meet :-)

Hi Martin,

Getting a bit tired/loopy, sorry for the incomplete example. Excuse will be sleep depravation if this goes KABOOM!, but should work.

Using late-binding, in a Standard Module:

Code:
Option Explicit
Sub ex()
    Call CloseTextFile("TimeKeeper.txt")
End Sub

Function CloseTextFile(Filename As String)
Dim WD As Object
Dim tks As Object
Dim t

    Set WD = CreateObject("Word.Application")
    Set tks = WD.Tasks
    
    For Each t In tks
        If t.Name Like Filename & "*" Then
            t.Close
            Exit For
        End If
    Next
    
    Set tks = Nothing
    WD.Quit
    Set WD = Nothing
End Function

Hope this helps,

Mark
 
Upvote 0
Just curious, why is this file open anyway? If another program is using it will we have rights to close it? Or will we potentially create a bad situation for the other program? Is it not being closed properly elsewhere...can we "fix" the problem at the root?

Text files can be opened in shared mode...so are we sure the file must be closed anyway? What is going wrong here, exactly?

Sorry - no answers, only questions! Alex
 
Upvote 0

Forum statistics

Threads
1,223,727
Messages
6,174,144
Members
452,547
Latest member
Schilling

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top