[RESOLVED] Best Possible Way To Save To A CSV...
file in vba word?
Ive used the active document function in vba (word). So when the user selects an answer the answer is shown on the current word docunment. But i dont want this, I want the answers to be saved on a CSV file on another location (can i change the location). Im not sure how this works does it get saved in a word file and then converted or does it get saved straight in to a csv file? I know im asking for alot but if anybody could help or send me in the correct direction i would appreciate it.
thanks
Re: Best Possible Way To Save To A CSV...
You can use basic File I/O to create and write to a file and delimit it with any character you desire. ;)
VB Code:
Open "C:\Test.csv" For Output As #1
Print #1, "1, This is my answer for the first question. Note the comma."
Close #1
Re: Best Possible Way To Save To A CSV...
I wouldn't try saving it in a Word file. That, by default, is .doc, not .csv. Office, by default, treats .csv as an Excel file, although you can open it in Word, Notepad, import into Access, etc. Anyway, Word often has hidden formatting that can give you unpredictable results with .csv files.
Also, .csv will treat all commas, or whatever character you choose, as delimiters. If there will be commas within a field, choose something else. We use the pipe symbol - | -, at another job they used some weird ascii character, ascii 136 or something, don't recall exactly.
Re: Best Possible Way To Save To A CSV...
lol im lost, ive entered the coding you specified in the document_open function, as i dont think it goes anywere else. I dont think it goes under the newmacro section or within the main coding. Currently the way i have done the coding for the userform is:
VB Code:
Option Explicit
Dim genders As String ‘This is at the top of the coding
Then within the option button I have more coding:
VB Code:
With ActiveDocument
If optmale = True Then genders = "M"
End With
The final part I have which is with the finish button is:
VB Code:
Selection.TypeText Text:=genders.
This is simple as the answer gets thrown in to the active word document, but how would I do this with a csv file. Also doesn’t your coding suggest there is already a csv file saved under the c: directory, or does it create one if there is no csv file?
Re: Best Possible Way To Save To A CSV...
Hi, I understand now. i saved the text under the "userform1.show" in the newmacro section. It is working fine, as i found the csv file under the directory were i saved it. But the problem im facing now is, how do i correspond the answers 2 the userform?
If you could help id be greatful, Thanks.
Re: Best Possible Way To Save To A CSV...
Also there is a list, so in total there are 15 questions how does the coding go about?
VB Code:
Open "C:\Test.csv" For Output As #1
Print #1, "1, This is my answer for the first question"
Close #1
Open "C:\Test.csv" For Output As #2
Print #2, "2, This is my answer for the second question"
Close #2
The problem with the above coding is the csv file only saved the last part
Re: Best Possible Way To Save To A CSV...
Either Print the whole file before closing or Open for Append after the first Open for Output. Your second Open statement is overwriting your first.
Re: Best Possible Way To Save To A CSV...
Thanks loads, could you suggest in anyway what i could do to save the answers i select on the userform please? the coding i gave above was my coding on a typical option button on the userform, by the way i was just wondering you know the coding with the outputs were does that go? does that go in the newmacro part or the actual coding? or even the this document part?
Sorry if i look dumb, its just i only really started working with vba in word since monday and as you can see i dont know much.
Re: [RESOLVED] Best Possible Way To Save To A CSV...
Resolved, amazing what you can you do when you really put your mind to it and think about it. Thanks every1 for their help