none
Save all rtf files in a folder as docx RRS feed

  • Question

  • Using another software to call a vb script that would open all rtf filles contained in a folder and save them as word docx files. I have a successful script that works with Excel converting xml to xlsx but am having trouble with a converting a similar script using word.  Here is my feeble attempt so far.

    Const wdFormatDocument = 0

    Set oWord = CreateObject("Word.Application")
    Dim fso,f 
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder("C\D\Output")
    For Each file In f.Files 
    If Right(LCase(f.Name), 4) = ".rtf" Then
    oWord.Open.Documents(f.Name)
    oWord.SaveAs Left(f, Len(f) - 5) & ".docx",wdFormatDocument
    Set f = Nothing
    End If
    oWord.Quit
    Set oWord = Nothing 
    Set fso = Nothing
    Next

    I am not a VB coder but trying to figure this out by looking at examples.  Any help is appreciated.
     
    Wednesday, August 31, 2011 6:47 PM

Answers

  • You made several mistakes:

    1. You accidentally quit Word and set oWord and fso to nothing INSIDE THE LOOP, so only the first iteration will work.
    2. You refer to f.Name or even just f instead of file.Path
    3. You call oWord.Open.Documents instead of oWord.Documents.Open
    4. You try to call SaveAs on oWord instead of on a document object
    5. You removed 5 instead of 4 characters when replacing ".rtf" with ".docx"

    Try this instead:

      Dim oWord, oDoc, fso, f
      
      Set oWord = CreateObject("Word.Application")
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFolder("somefolderpath")
      
      For Each file In f.Files
        If Right(LCase(file.Name), 4) = ".rtf" Then
          Set oDoc = oWord.Documents.Open(file.Path)
          oDoc.SaveAs Left(file.Path, Len(file.Path) - 4) & ".docx", wdFormatDocument
        End If
      Next
      
      oWord.Quit
      Set oWord = Nothing
      Set fso = Nothing
    

     


    jmh
    Wednesday, August 31, 2011 7:47 PM

All replies

  • You made several mistakes:

    1. You accidentally quit Word and set oWord and fso to nothing INSIDE THE LOOP, so only the first iteration will work.
    2. You refer to f.Name or even just f instead of file.Path
    3. You call oWord.Open.Documents instead of oWord.Documents.Open
    4. You try to call SaveAs on oWord instead of on a document object
    5. You removed 5 instead of 4 characters when replacing ".rtf" with ".docx"

    Try this instead:

      Dim oWord, oDoc, fso, f
      
      Set oWord = CreateObject("Word.Application")
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.GetFolder("somefolderpath")
      
      For Each file In f.Files
        If Right(LCase(file.Name), 4) = ".rtf" Then
          Set oDoc = oWord.Documents.Open(file.Path)
          oDoc.SaveAs Left(file.Path, Len(file.Path) - 4) & ".docx", wdFormatDocument
        End If
      Next
      
      oWord.Quit
      Set oWord = Nothing
      Set fso = Nothing
    

     


    jmh
    Wednesday, August 31, 2011 7:47 PM
  • Thanks for the help, however, when I ran this from my external program, I received this error message: Loading script "C:\test.vbs" failed because the process cannot access the file because it is being used by another process.  Is there something that needs slight modification to not get this error?
    WCC
    Thursday, September 1, 2011 8:26 PM
  • That doesn't sound like an error with the script per se. It just sounds like you're trying to open the same document with multiple programs at the same time. If a VB script halts it can leave an instance of Word open in the background, which will keep a lock on any files that Word instance has opened. Check Task Manager and kill any instances of WINWORD that are still running, then try the script again.


    jmh
    Thursday, September 1, 2011 8:33 PM
  • There must be some issue with the program I use (SAS) when it creates and calls the VB Script from the program.  When I run the script from file explorer the script works beautifully.  Thanks for your help.
    WCC
    Friday, September 2, 2011 11:51 AM