rms-wordofficer.doc

Can you convert .docm files to .docx using R?


I have about 100 .docm files that I need to convert to .docx. I am doing this because I am importing tables from the .docx files using the officer library (this does not work with .docm).


Solution

  • Basically what you're looking for seems to be "how can I convert a bunch word of documents from having macro enabled to not having macro enabled using R".

    I'll say it as is: Probably cant without venturing into the RDComClient package. I'd say it's easier to create a seperate word file, and use a quick VBA script to do the job (such as the convert_files function below).

    Function GetFolderFiles(folder As String) As Variant
        ' Extract files from folder
        Dim oFSO As Object
        Dim oFolder As Object
        Dim oFile As Object
        Dim i As Long
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oFolder = oFSO.GetFolder(folder)
        Dim result() As Variant
        ReDim result(0 To oFolder.files.Count - 1)
        i = 0
        For Each oFile In oFolder.files
            If InStr(1, oFile.Name, "docm") Then
                result(i) = oFile.Path
                i = i + 1
            End If
        Next oFile
        ReDim Preserve result(0 To i - 1)
        GetFolderFiles = result
    End Function
    Sub convert_files(Optional ByVal folder As String = "path-to-folder")
        ' Iterate through docm files in a folder and save them as docx
        Dim files As Variant
        files = GetFolderFiles(folder)
        Dim i As Variant
        Dim wordDoc As Word.Document
        On Error GoTo safeExit
        For Each i In files
            ' Open Document (invisibly for speed)
            Set wordDoc = Word.Documents.Open(i, Visible:=False, ReadOnly:=True)
            ' Save document replacing docm with docx
            wordDoc.SaveAs2 Replace(i, "docm", "docx"), FileFormat:=wdFormatDocumentDefault   ' replace docm with docx
            ' Close the document
            wordDoc.Close wdDoNotSaveChanges
        Next i
        Exit Sub
    safeExit:
        ' In case we stop early due to an error or esc press we want to ensure no invisible documents hang around.
        On Error Resume Next
        wordDoc.Close
        On Error GoTo 0
        MsgBox "Error happened did not finish after file '" & i & "'.", vbExclamation
    End Sub
    

    Once you have this defined in a saved document you can then follow up by running it from R either through cmd or using RDComclient using this example.