我有大約 100 個 .docm 檔案需要轉換為 .docx。我這樣做是因為我使用官員庫從 .docx 檔案匯入表(這不適用于 .docm)。謝謝!
uj5u.com熱心網友回復:
基本上,您正在尋找的似乎是“如何將一堆檔案從啟用宏轉換為不使用 R 啟用宏”。
我會按原樣說:可能不能不冒險進入 RDComClient 包。我會說創建一個單獨的 word 檔案更容易,并使用快速 VBA 腳本來完成這項作業(例如convert_files下面的函式)。
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
一旦在保存的檔案中定義了它,您就可以通過 cmd 或使用本示例使用 RDComclient 從 R 運行它來跟進。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383115.html
下一篇:將字串拆分為單獨的列R
