Storing a vba generated text file to the current directory
I have several text boxes in a Word document. Each input to a text box is stored as a variable.
The code below allows me to create a text file that includes the values input to the text boxes by reading the text box variable. However … I need to be able to set the folder for the text file to whatever the current directory is. That code follows below the first block of code. You can see the variables are the same, the only thing I did was create a path to the current directory. When I check the local variables the path is correct. Yet I get an error: Method or Data Member not found. The highlighted code associated with the error is: Me.txtQuestion01.
So … It works with a path that is fixed, but when I identify the path programatically, it does not.
Please help! I am already old and this is making me older by the minute.
_________________________________________
Private Sub txtQuestion01_Change()
Me.txtAnswer01.Value = Me.txtQuestion01.Value
End Sub
Private Sub Document_Close()
Dim record As String
Open "C:\Users\rjudge\SkyDrive\Programming\SAP Assignment grading\MS Word files\Ch-03-01.txt" For Output As #1
Print #1, "Question #1 " & Me.txtQuestion01.Value
Print #1, "Question #2 " & Me.txtQuestion02.Value
Print #1, "Question #3 " & Me.txtQuestion03.Value
Print #1, "Question #4 " & Me.txtQuestion04.Value
Print #1, "Question #5 " & Me.txtQuestion05.Value
Print #1, "Question #6 " & Me.txtQuestion06.Value
Close #1
End Sub
_____________________________________________
New code with code to determine current directory
_____________________________________________
Private Sub Document_Close()
Dim record As String
Dim strFileName As String
Dim pathname As String
If UCase(Right(ActiveDocument.Name, 1)) = "X" Then
'.docx
strFileName = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 5)
Else
'.doc
strFileName = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
End If
pathname = ActiveDocument.Path & "\" & strFileName & "txt"
'write the txt file
Open pathname For Output As #1
Print #1, "Question #1 " & Me.txtQuestion01.Value
Print #1, "Question #2 " & Me.txtQuestion01.Value
Print #1, "Question #2 " & Me.txtQuestion02.Value
Print #1, "Question #2 " & Me.txtQuestion03.Value
Print #1, "Question #2 " & Me.txtQuestion04.Value
Close #1
End Sub