vba - Removing extra quotes in XML files -
i'm making quick , dirty xml generator. below code works when open it has additional double quotes i.e. <"something"> turn "<""something"">. there way remove them below code? can't use xltextprinter because of cells have more 255 characters. appreciated!
sub test() dim desktop string dim filename string desktop = createobject("wscript.shell").specialfolders("desktop") activesheet filename = .range("b1").value .range("h2:k33").copy workbooks.add activeworkbook.sheets(1).range("a1").pastespecial xlpastevalues application.cutcopymode = false end activeworkbook .saveas filename:=desktop & application.pathseparator & filename _ , fileformat:=xltextmsdos, createbackup:=false .close savechanges:=false end end sub
because excel add quotes file in situations when exported text have couple options.
chr(9)
tab
chr(34)
quote
vbcrlf
new line character
instead of saving file write text directly:
dim desktop string dim filename string dim fs object dim f object dim integer set fs = createobject("scripting.filesystemobject") desktop = createobject("wscript.shell").specialfolders("desktop") activesheet filename = desktop & application.pathseparator & .range("b1").value set f = fs.createtextfile(filename & ".txt", true, false) = 2 33 f.write (.cells(i, 8) & chr(9) & .cells(i, 9) & chr(9) & .cells(i, 10) & chr(9) & .cells(i, 11) & vbcrlf) next f.close set f = nothing set fs = nothing end
or siddarth rout suggested can open file after creating , replace additional quotes adds:
dim desktop string dim filename string desktop = createobject("wscript.shell").specialfolders("desktop") activesheet filename = desktop & application.pathseparator & .range("b1").value .range("h2:k33").copy workbooks.add activeworkbook.sheets(1).range("a1").pastespecial xlpastevalues application.cutcopymode = false end with activeworkbook .saveas filename:=filename _ , fileformat:=xltextmsdos, createbackup:=false .close savechanges:=false end dim fs object dim f object dim content string set fs = createobject("scripting.filesystemobject") set f = fs.opentextfile(filename & ".txt", 1, -2) content = f.readall f.close content = replace(content, chr(34) & "<", "<") content = replace(content, ">" & chr(34), ">") content = replace(content, chr(34) & chr(34), chr(34)) set f = fs.createtextfile(filename & ".txt", true, false) f.write (content) f.close set f = nothing set fs = nothing
Comments
Post a Comment