Ok heres the deal I have like 500 .doc documents and I need them to be .txt files i can't just highlight them all and click save as .txt so I need a macro that will open a file, save it as a .txt file, close it, and open the next one, and repeat over and over any suggestions?
Last edited by eddielong; 07-12-2011 at 12:03 PM.
Not tested but try this:
Sub LoopFiles() Dim strDir As String, strFileName As String strDir = "C:\" strFileName = Dir(strDir & "*.doc") Do While strFileName <> "" Documents.Open strDir & strFileName strFileName = Left(strFileName, Len(strFileName) - 4) & ".txt" ActiveDocument.SaveAs FileName:=strDir & strFileName, fileformat:=wdFormatText ActiveDocument.Close strFileName = Dir Loop End Sub
Dom
Last edited by Domski; 07-11-2011 at 09:03 AM.
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
probably faster alternative:
sub snb() c00="E:\OF\" c01=dir(c00 & "*.doc") CreateObject ("word.application") do until c01="" with getobject(c00 & c01) c02=c02 & c01 c03=c03 & "~|" & .content .activate .close 0 end with c01=dir loop if c02<>"" then sn=split(replace(c02,".doc",".txt|"),"|") sp=split(mid(c03,3),"~|") for j=0 to ubound(sn) open c00 & sn(j) for output as #1 print #1,sp(j) close next end if End Sub
Last edited by snb; 07-12-2011 at 04:01 AM.
neither of those worked
Don't mention it.
Dom
Last edited by Domski; 07-12-2011 at 02:52 AM.
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Thank you for your concise and detailed feedback.
FWIW I just tested the code I posted and it happily crunched through all the Word docs in a folder converting them to text files.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Did you also test mine ?
Hi snb,
I just did and it ran but no text files to be seen.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
I forgot an asterisk.
Amended code:
sub snb() c00="E:\OF\" c01=dir(c00 & "*.doc") CreateObject ("word.application") do until c01="" with getobject(c00 & c01) c02=c02 & c01 c03=c03 & "~|" & .content .activate .close 0 end with c01=dir loop if c02<>"" then sn=split(replace(c02,".doc",".txt|"),"|") sp=split(mid(c03,3),"~|") for j=0 to ubound(sn) open c00 & sn(j) for output as #1 print #1,sp(j) close next end if End Sub
I get an error "Path Not Found" on this line now:
Open c00 & sn(j) For Output As #1
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
What value did you attribute to c00 ?
Did you end it with a backslash ?
if you searched for docx-files you should also change
sn=split(replace(c02,".docx",".txt|"),"|")
Last edited by snb; 07-12-2011 at 07:19 AM.
Guys it doesnt work i think the reason y it doesnt is because the files are inside a folder named Branham SW455S 3 Jun11on a usb named CENTON (Gplease correct anything that would need correcting sorry for not explaining that part
thats not suppose to be a smile thats an ":" and a ")"
I think you must be able to adapt the code yourself, at least you are permitted to do so..
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks