PDA

View Full Version : Storing a vba generated text file to the current directory



rjudge
01-31-2013, 11:11 AM
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

fumei
01-31-2013, 11:59 AM
You need a dot before "txt"


pathname = ActiveDocument.Path & "\" & strFileName & "txt"

has no dot.

If you do a debug.print for strFileNasme you will notice it has no trailing ".", so you need to add that.


pathname = ActiveDocument.Path & "\" & strFileName & ".txt"

DawnBarr
03-19-2020, 01:03 AM
The centre of the punctuation is filed for the individuals. The chapter of the mode and Dissertation Today (https://dissertation-today.com/) is ensured for the mid of the payments. The chunk is followed for the individuals. The struggle is done for all industries. The mark is placed for the team. plant is done for all humans for the middle of towpaths for all humans.