RSS   



  可打印版本 | 推薦給朋友 | 訂閱主題 | 收藏主題 | 純文字版  


 


 
主題: [其他] [求助]如何用程式(如:VBA或python)將DOC自動轉檔DOCX   字型大小:||| 
acrd
白銀驢友
等級: 15等級: 15等級: 15等級: 15等級: 15


今日心情

 . 積分: 1459
 . 文章: 1186
 . 收花: 12337 支
 . 送花: 2486 支
 . 比例: 0.2
 . 在線: 2435 小時
 . 瀏覽: 15472 頁
 . 註冊: 7988
 . 失蹤: 3
#1 : 2023-3-5 12:32 AM     全部回覆 引言回覆

chatgpt

Function GetAllDocFiles(folderPath As String) As String()
    ' 搜尋指定目錄及其子目錄下所有 .doc 檔案的路徑
    Dim fileNames() As String
    ReDim fileNames(0)

    Dim fileName As String
    fileName = Dir(folderPath & "\*.doc", vbNormal)

    While fileName <> ""
        If fileName <> "." And fileName <> ".." Then
            ' 將找到的 .doc 檔案加入陣列中
            fileNames(UBound(fileNames)) = folderPath & "\" & fileName
            ReDim Preserve fileNames(UBound(fileNames) + 1)
        End If
        fileName = Dir()
    Wend

    Dim subFolderPath As String
    subFolderPath = Dir(folderPath & "\*", vbDirectory)

    While subFolderPath <> ""
        If subFolderPath <> "." And subFolderPath <> ".." Then
            ' 如果是目錄,就遞迴呼叫本身搜尋該目錄
            If (GetAttr(folderPath & "\" & subFolderPath) And vbDirectory) = vbDirectory Then
                Dim subFolderFiles() As String
                subFolderFiles = GetAllDocFiles(folderPath & "\" & subFolderPath)
                If UBound(subFolderFiles) > -1 Then
                    ' 將子目錄中找到的 .doc 檔案加入陣列中
                    Dim i As Integer
                    For i = 0 To UBound(subFolderFiles)
                        fileNames(UBound(fileNames)) = subFolderFiles(i)
                        ReDim Preserve fileNames(UBound(fileNames) + 1)
                    Next i
                End If
            End If
        End If
        subFolderPath = Dir()
    Wend

    If UBound(fileNames) > 0 Then
        ReDim Preserve fileNames(UBound(fileNames) - 1)
    Else
        ReDim fileNames(-1)
    End If
   
    GetAllDocFiles = fileNames
End Function

Sub ConvertDocToDocx()
    ' 將所有 .doc 檔案轉換為 .docx 檔案
    Dim fileNames() As String
    fileNames = GetAllDocFiles("C:\My Documents") ' 指定搜尋目錄

    Dim fileName As String
    For Each fileName In fileNames
        Dim doc As Document
        Set doc = Documents.Open(fileName)
        doc.SaveAs2 FileName:=Replace(fileName, ".doc", ".docx"), FileFormat:=wdFormatXMLDocument
        doc.Close
    Next fileName
End Sub


這個程式會搜尋指定的目錄及其子目錄下所有 .doc 檔案,並將其轉換為 .docx 格式。如果您希望搜尋的目錄不同,請將 GetAllDocFiles 函數中的 folderPath 變數設定為該目錄的路徑。注意,這個程式會將找到的所有 .doc 檔案都轉換為 .docx 格式,請務必備份原始檔案。

[acrd 在  2023-3-5 12:44 AM 作了最後編輯]



[如果你喜歡本文章,就按本文章之鮮花~送花給作者吧,你的支持就是別人的動力來源]
本文連接  
檢閱個人資料  發私人訊息  Blog  快速回覆 新增/修改 爬文標記

   

快速回覆
表情符號

更多 Smilies

字型大小 : |||      [完成後可按 Ctrl+Enter 發佈]        

溫馨提示:本區開放遊客瀏覽。
選項:
關閉 URL 識別    關閉 表情符號    關閉 Discuz! 代碼    使用個人簽名    接收新回覆信件通知
發表時自動複製內容   [立即複製] (IE only)


 



所在時區為 GMT+8, 現在時間是 2024-4-16 01:31 PM
清除 Cookies - 連絡我們 - TWed2k © 2001-2046 - 純文字版 - 說明
Discuz! 0.1 | Processed in 0.018105 second(s), 7 queries , Qzip disabled