ms word - Convert .rtf to .docx from selected folder and its subfolders in VBA -
i managed modify vba script able choose folder, convert .rtf .docx, , delete .rtf files after conversion. cant figure out, is; how script convert subfolders in-within folder. have looked online , not able find solution. please advise.
sub changertftodocxortxtorrtforhtml() dim fs object dim ofolder object dim tfolder object dim ofile object dim strdocname string dim intpos integer dim folderdialog filedialog dim filetype string dim locfolderkill string set folderdialog = application.filedialog(msofiledialogfolderpicker) folderdialog.allowmultiselect = false folderdialog.show debug.print folderdialog.selecteditems(1) select case application.version case < 12 filetype = ucase(inputbox("change rtf txt, rtf, html, docx", "file conversion", "docx")) loop until (filetype = "txt" or filetype = "rtf" or filetype = "html" or filetype = "docx") case >= 12 filetype = ucase(inputbox("change rtf txt, rtf, html, docx or pdf(2007+ only)", "file conversion", "docx")) loop until (filetype = "txt" or filetype = "rtf" or filetype = "html" or filetype = "pdf" or filetype = "docx") end select application.screenupdating = false set fs = createobject("scripting.filesystemobject") set ofolder = fs.getfolder(folderdialog.selecteditems(1)) each ofile in ofolder.files dim d document set d = application.documents.open(ofile.path) strdocname = activedocument.name intpos = instrrev(strdocname, ".") strdocname = left(strdocname, intpos - 1) changefileopendirectory ofolder select case filetype case = "docx" strdocname = strdocname & ".docx" activedocument.saveas filename:=strdocname, fileformat:=wdformatxmldocument case = "txt" strdocname = strdocname & ".txt" activedocument.saveas filename:=strdocname, fileformat:=wdformattext case = "rtf" strdocname = strdocname & ".rtf" activedocument.saveas filename:=strdocname, fileformat:=wdformatrtf case = "html" strdocname = strdocname & ".html" activedocument.saveas filename:=strdocname, fileformat:=wdformatfilteredhtml case = "pdf" strdocname = strdocname & ".pdf" end select d.close changefileopendirectory ofolder next ofile application.screenupdating = true 'this delete .rft files in same folder. kill "*.rtf" end sub
here's example of how check subfolders - can incorporate existing code looping on collection returned getfilematches
sub tester() dim col collection, f set col = getfilematches("c:\_stuff\test\", "*.txt") each f in col debug.print f.path next f end sub 'return collection of file objects given starting folder , file pattern ' e.g. "*.txt" 'pass false last parameter if don't want check subfolders function getfilematches(startfolder string, filepattern string, _ optional subfolders boolean = true) collection dim fso, fldr, f, subfldr dim colfiles new collection dim colsub new collection set fso = createobject("scripting.filesystemobject") colsub.add startfolder while colsub.count > 0 set fldr = fso.getfolder(colsub(1)) colsub.remove 1 each f in fldr.files if ucase(f.name) ucase(filepattern) colfiles.add f next f if subfolders each subfldr in fldr.subfolders colsub.add subfldr.path next subfldr end if loop set getfilematches = colfiles end function
Comments
Post a Comment