+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: change .doc to .txt

  1. #1
    Registered User
    Join Date
    06-29-2011
    Location
    tennessee, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    change .doc to .txt

    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.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: change .doc to .txt

    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.

  3. #3
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: change .doc to .txt

    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.



  4. #4
    Registered User
    Join Date
    06-29-2011
    Location
    tennessee, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: change .doc to .txt

    neither of those worked

  5. #5
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: change .doc to .txt

    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.

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: change .doc to .txt

    Thank you for your concise and detailed feedback.



  7. #7
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: change .doc to .txt

    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.

  8. #8
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: change .doc to .txt

    Did you also test mine ?



  9. #9
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: change .doc to .txt

    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.

  10. #10
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: change .doc to .txt

    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



  11. #11
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: change .doc to .txt

    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.

  12. #12
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: change .doc to .txt

    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.



  13. #13
    Registered User
    Join Date
    06-29-2011
    Location
    tennessee, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: change .doc to .txt

    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 (G please correct anything that would need correcting sorry for not explaining that part

  14. #14
    Registered User
    Join Date
    06-29-2011
    Location
    tennessee, USA
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: change .doc to .txt

    thats not suppose to be a smile thats an ":" and a ")"

  15. #15
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: change .doc to .txt

    I think you must be able to adapt the code yourself, at least you are permitted to do so..



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0