If you feel adventurous, here is a solution that will give you pleasure. Start with a standard code module. By default it will be called Module1
. Place the following two procedures in that module. It doesn't matter what you call the module, but be sure not to use the ThisWorkbook
code module, nor any of the code modules named after the worksheets.
The function 'SavedDataFileName' is the place where you declare the location where you want to store the data to be remembered. You can use any location you like. the code specifies a location like C:\Users\[Your Name]
. You can also change the file name. The point is that all this is stored in one place from where it is retrieved when the file is read and when it is written.
Option Explicit
Function SavedDataFileName() As String
' create this as a function to be called by various parts of your code
' so that you don't have to repeat it in many places in case of future change
SavedDataFileName = Environ("USERPROFILE") & "\SavedPath.txt"
End Function
The next function reads the text file just specified. Actually, this is code I had ready. Therefore it has the capability to read many data. You only want to read one - the path. It will do that.
Function TextFile(Ffn As String, _
MaxLines As Integer) As String()
' 17 Oct 2016
Dim Fun() As String ' Function return
Dim i As Integer
Dim Fid As Integer ' File ID
If Len(Dir(Ffn)) Then
ReDim Fun(MaxLines) ' arbitrary maximum
Fid = FreeFile()
Open Ffn For Input As #Fid
While Not EOF(Fid)
Line Input #Fid, Fun(i)
Fun(i) = Trim(Fun(i))
i = i + 1
Wend
Close #Fid
ReDim Preserve Fun(i - 1)
End If
TextFile = Fun
End Function
Now please go to the code sheet of your form on which you want the data to be preserved. All the following procedures must be on that form. Don't install them anywhere else. They can only work when installed on that particular code sheet.
The first procedure runs when the form is initialised, meaning when it is first created - on startup.
Option Explicit
Private Sub UserForm_Initialize()
' 13 May 2017
Dim SavedData() As String
On Error GoTo EndRetrieval
SavedData = TextFile(SavedDataFileName, 10)
TextBox1.Text = SavedData(0)
' you can pre-load more controls in your form here
EndRetrieval:
End Sub
In this sub the text file is opened and data imported from it. A maximum of 10 data are expected. You can set this number higher or lower. The higher you set it the more memory space will temporarily be used. You only need 1 data item. That will have the index number 0 (zero), and it is assigned to TextBox1. You can change that, of course. If an error is encountered nothing will be done and the text box remains blank.
The next procedure will run whenever you exit from TextBox1. Of course, you can change its name to refer to another text box. Its purpose is to ensure that the textbox contains a valid path name. If the user enters something that isn't a valid name he will be told so.
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' 13 May 2017
Dim Ffn As String ' Full file name
Ffn = Trim(TextBox1.Text)
On Error Resume Next
' For testing MsgMe is a sub containing a MsgBox.
' MsgMe Dir(Ffn & "\", vbDirectory) & ", " & Len(Dir(Ffn & "\", vbDirectory))
Cancel = (Len(Ffn) = 0) Or (Len(Dir(Ffn & "\", vbDirectory)) = 0)
If Not Cancel Then Cancel = CBool(Err.Number) ' in case Dir(Ffn) caused an error
If Cancel Then
MsgBox "The path you entered isn't valid." & vbCr & _
"Please enter a valid path."
Else
TextBox1.Text = Ffn ' removed leading/trailing blanks
End If
End Sub
The final procedure runs when the form is closed. It write the current setting of TextBox1 to the text file from where it will be retrieved when the form is next loaded.
Private Sub UserForm_Terminate()
' 12 May 2017
Open SavedDataFileName For Output As #1
Print #1, TextBox1.Text
' you can write more data to be remembered here
Close #1
End Sub
This procedure writes only a single line to the text file. If you want to save more items just print more values to the file. Observe that the quality of the data saved isn't checked. That was done after it was entered. If the user managed to smuggle a faulty path into the text box that faulty path will come back to haunt him the next morning.