Export Excel workseet into txt file using vba - (text and numbers with formulas) -
i new vba have developed code use in excel move text/numbers txt file. issue have 3 functions need go same txt file. when run first function has
set fs = createobject("scripting.filesystemobject") set = fs.createtextfile(module1.nys45uploadfilename, true) a.writeline (str) a.close.
the other 2 functions had save.
set fs = createobject("scripting.filesystemobject") set = fs.savetextfile(module1.nys45uploadfilename, true) a.writeline (str) a.close.
the first function go txt file, other 2 error
object doesn't support property or method.
when changed 3 create last function goes txt file. can not figure out word use in order make other 2 functions follow txt file in order keyed.
the entire code follows
private sub addrecord_click() on error goto errhandler module1.nys45uploadfilename = application.getsaveasfilename(filefilter:="textfiles (*.txt), *.txt") if module1.nys45uploadfilename = "false" exit sub header_rec detail_rec1 detail_rec2 dim strmsg string strmsg = "your file has been added here: " & module1.nys45uploadfilename msgbox strmsg exit sub errhandler: msgbox err.number & vbnewline & err.description resume next end sub function header_rec() dim str, strfilename, txtpath string dim strlencount, strspacer integer str = str & range("a3").value str = str & range("b3").value str = str & trim(range("c3").value) strlencount = len(trim(range("c3").value)) strspacer = 30 - strlencount str = module1.spaceadd(str, strspacer) str = str & range("d3").value str = str & range("e3").value str = module1.spaceadd(str, 159) ' debug.print len(str) set fs = createobject("scripting.filesystemobject") set = fs.createtextfile(module1.nys45uploadfilename, true) a.writeline (str) a.close ' debug.print str end function function detail_rec1() dim str, strnum, str2, strfilename, txtpath string dim strlencount, strspacer integer if range("a7").value <> "5" exit function end if str = str & range("a7").value str = str & range("b7").value str = str & range("c7").value str = module1.spaceadd(str, 1) str = str & trim(range("d7").value) strlencount = len(trim(range("d7").value)) strspacer = 30 - strlencount str = module1.spaceadd(str, strspacer) str = str & range("e7").value ' debug.print len(str) set fs = createobject("scripting.filesystemobject") set = fs.addtextfile(module1.nys45uploadfilename, true) a.writeline (str) a.close ' debug.print str end function function detail_rec2() dim str, strnum, str2, strfilename, strnew, txtpath string dim strlencount, strspacer integer if range("a11").value <> "6" exit function end if str = str & range("a11").value str = str & range("b11").value str = str & trim(range("c11").value) strlencount = len(trim(range("c11").value)) strspacer = 11 - strlencount str = module1.spaceadd(str, strspacer) strspacer = 30 - strlencount str = module1.spaceadd(str, strspacer) str = str & range("f11").value str = str & range("g11").value ' debug.print len(str) set fs = createobject("scripting.filesystemobject") set = fs.gettextfile(module1.nys45uploadfilename, true) a.writeline (str) a.close ' debug.print str end function
consider below example showing how write text file using opentextfile
method:
sub test() const forwriting = 2 const forappending = 8 const formatdefault = -2 const formatunicode = -1 const formatascii = 0 strfile = "c:\testfile.txt" strtext = "new unicode text file created, first line." & vbcrlf writetextfile strfile, strtext, forwriting, formatunicode strtext = "new line added, second line." & vbcrlf writetextfile strfile, strtext, forappending, formatunicode strtext = "one more line." & vbcrlf writetextfile strfile, strtext, forappending, formatunicode end sub sub writetextfile(strpath, strcontent, lngmode, lngformat) ' strpath: path text file ' strcontent: text written file ' lngmode: 2 - writing, 8 - appending ' lngformat: -2 - system default, -1 - unicode, 0 - ascii createobject("scripting.filesystemobject").opentextfile(strpath, lngmode, true, lngformat) .write strcontent .close end end sub
Comments
Post a Comment