Closing Text File that is open

craigachan

Registered User.
Local time
Today, 04:57
Joined
Nov 9, 2007
Messages
285
I have a Test.txt file that is already open. I want to close it with the following code:

Code:
     Dim objfso
     Dim objfile
     dim strfilepath as string
     strfilepath = "C:\mydir\test.txt"

      '===Following does not close txt file
     Set objfso = CreateObject("Scripting.FileSystemObject")
     Set objfile = objfso.OpenTextFile(strfilepath, 1)
     objfile.Close

Can someone tell me why this code does not work?

Thank you.
 
what do you want?
open the file for processing data recs,
or open file for you to read it (like in notepad)?

for data reading...:
open filename for input as #1
'read record
close 1

to open it in notepad,
use the shell command
 
Thanks for your reply. I already opened it in a previous routine, but now I want to just close the file. My opening routine opens it in notepad.
 
I already opened it in a previous routine

The question becomes, where is the FileSystem Object located through which you opened the file? That is the only place where the FSO.Close will work right.

If you build a General module with your FSO stuff in the declarations area of said module, then you can open and close FSO-related files from anywhere. If the FSO file was opened from a Class module, it can only be closed from the same Class module. Further, if you close the form/report associated with the Class Module, the next time you open the form/report, it is a different instantiation of the document-object and might not have a clue about the file in question.

There are keywords to allow you to do this from a Class module such that the FSO data structures persist, but to my way of thinking, the better solution is to just put the whole schmeer in a General module. You could then use that module as a place to hold your FSO-related subroutines, too.
 
Thanks for your reply. Here is how I open the textfile:

Code:
Public Function OpenPharmTxtFile(strPharmTxt As String)
On Error GoTo OpenPharmTxtFileErr

    Dim path2file As String     'full path to file and filename
    Dim y As Long
    Dim tempstring As String, strchar2add As String
    '===Remove '#' from text
    For y = 1 To Len(strPharmTxt)
        If Mid(strPharmTxt, y, 1) <> "#" Then
            strchar2add = Mid(strPharmTxt, y, 1)
        Else
            strchar2add = ""
        End If
        If Len(tempstring) < 1 Then
            tempstring = strchar2add
        Else
            tempstring = tempstring & strchar2add
        End If
    Next y
    strPharmTxt = tempstring
    path2file = DLookup("tempDocs", "Chanlinkpaths") & "PharmTxtFiles\" & strPharmTxt & ".txt"
    Forms!ChartPrescription!PharmTxtFile = path2file        'store the path/textfilename on form to close later.

    If DoesFileExist(path2file) <> True Then
        Call WriteToTxtFile(strPharmTxt, path2file) 'ChartPublic
    End If
    
    Shell "notepad '" & path2file & "'", vbMinimizedNoFocus


OpenPharmTxtFileExit:
    Exit Function
OpenPharmTxtFileErr:
    MsgBox "ChartPublic-OpenPharmTxtFile: " & Err.Number & " - " & Err.Description
    Resume OpenPharmTxtFileExit
End Function

Here is the WriteToTxtFile code

Code:
Public Function WriteToTxtFile(strData As String, strPathFileName As String)
On Error GoTo WriteToTxtFileErr
'Writes data to .txt file and creates and saves to patient directory
    Dim ifilenumber As Long
    ifilenumber = FreeFile()
    Open strPathFileName For Output As #ifilenumber
    Print #ifilenumber, strData
    Close #ifilenumber


WriteToTxtFileExit:
    Exit Function
WriteToTxtFileErr:
    MsgBox "ChartPublic-WriteToTxtFile: " & Err.Number & " - " & Err.Description
    Resume WriteToTxtFileExit
End Function
 
Use the Scripting.FileSystemObject and the "OpenTextFile" for it all, Creating/opening the file, writing to the file and closing the file. It is much easier to maintain in the future + it would reduce a lot of your code.
The link show the description of it + some sample code.
https://msdn.microsoft.com/en-us/library/Aa265347(v=VS.60).aspx?f=255&MSPPError=-2147217396
Use the replace function to get rid of the "#" sign instead of the looping.
http://www.techonthenet.com/access/functions/string/replace.php
 
Thanks to you both for your replies. But I'm an amateur VBAer and what you are both saying is way over my head.

I basically don't really understand the way I opened it. Can you give me an example of how the code goes for me to close a text file. It appears that I need to use a certain way to close it since I opened it a certain way (this I don't fully understand). Can you give me example code for me to close it based on the way I opened it? Or point out where I am going wrong?

Thank you.
 
The sample code is in the link + description, did you read it?
If your self have put together the code you shown, it shouldn't be a problem to understand the example.
 
Confusing post. You did not "open a text file". You started an application which in turns opens the file - an entirely diffrerent story.

You can close it by killing the process/window. Se fx post 23 in this thread: http://www.access-programmers.co.uk/forums/showthread.php?t=279235&highlight=close&page=2

But this entire undertaking is suspect: why not let people close the file themselves. If it is for display only, use a form to show the text.
 
Okay, here is how I did it. When opening the txt file I kept track of the hWnd (windows handle) and the name of the txt file (strFiletitle - the name of the file when I created it), and put it in a field on my form. I opened, as noted above using:

Shell "notepad '" & path2file & "'", vbMinimizedNoFocus

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Long
Public Function GetWindowsHandle(strFileTitle As String)
'====Used to get the windows handle when opening txt file so that you can close it later based on the windows handle hWnd
    Dim hWnd As Long
    hWnd = FindWindow("Notepad", strFileTitle)
    If hWnd Then
        GetWindowsHandle = hWnd
    End If
End Function

Then later when I want to close the txt file, I close the txt file using the hWnd saved on the form using:

Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060

Public Sub Close_VB_Window(lghandle As Long)
    Dim hWnd As Long
    hWnd = lghandle
    If hWnd <> 0 Then
        SendMessage hWnd, WM_SYSCOMMAND, SC_CLOSE, 0
    End If
End Sub

To check the code, I opened other txt files to make sure only the one I intended to close actually closed. That is, I dont' want to close an unintended txt file that is open.

I'm not sure this is the correct way, but it works for my application.

Thanks everyone for your ideas and support and hope this helps others.
 
put
close 1 , in the front too.
Code:
close 1
open filename for input as #1
'read record
close 1
 

Users who are viewing this thread

Back
Top Bottom