ms word - VBA script convert folder of .rtf files to .docx files - two minor problems -
i use vba script (see below) in word 2013 convert folder of .rtf files .docx files. works, has 2 minor problems.
- i have acknowledge each original file .rtf file. when word opens each .rtf file there's dialog requires me confirm each file .rtf file.
- when view converted .docx files in word there's "compatibility mode" header, suggests haven't converted.
are there fixes these problems? first 1 kind of undermines whole point of scripting , i'm afraid second 1 cause unforeseen problems.
sub convertrtftodocx() set oword = createobject("word.application") application.filedialog(msofiledialogfolderpicker) .title = "select folder..." .show myfolder = .selecteditems.item(1) end mywildcard = inputbox(prompt:="enter wild card...") mydocs = dir(myfolder & "\" & mywildcard) while mydocs <> "" debug.print mydocs set odoc = oword.documents.open(myfolder & "\" & mydocs) odoc.saveas myfolder & "\" & left(mydocs, len(mydocs) - 4) & ".docx", _ wdformatxmldocument mydocs = dir() wend oword.quit end sub
the following code works.
sub convertrtftodocx() application.filedialog(msofiledialogfolderpicker) .title = "select folder..." .show myfolder = .selecteditems.item(1) end mywildcard = inputbox(prompt:="enter wild card...") mydocs = dir(myfolder & "\" & mywildcard) while mydocs <> "" documents.open filename:=myfolder & "\" & mydocs, confirmconversions:=false activedocument.saveas2 filename:=myfolder & "\" & left(mydocs, len(mydocs) - 4) & ".docx", _ fileformat:=wdformatdocumentdefault, _ compatibilitymode:=wdcurrent activedocument.close savechanges:=false mydocs = dir() wend end sub
i did restructuring (e.g., use activedocument
instead of creating own object), real changes did to
- set
confirmconversions:=false
on open - use
saveas2
method , setfileformat:=wdformatdocumentdefault
,compatibilitymode:=wdcurrent
i guess both of these can set default (i'm overwhelmed office options , leave defaults).
Comments
Post a Comment