且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

循环遍历目录中的所有 Word 文件

更新时间:2023-09-17 16:20:34

您实际上并不需要 WordtoTxtwLB 宏.您可以组合这两个代码.看这个例子

You don't actually need the WordtoTxtwLB Macro. You can combine both the codes. see this example

(未经测试)

Sub LoopDirectory()
    Dim vDirectory As String
    Dim oDoc As Document

    vDirectory = "C:programs2	est"

    vFile = Dir(vDirectory & "*.*")

    Do While vFile <> ""
        Set oDoc = Documents.Open(fileName:=vDirectory & vFile)

        ActiveDocument.SaveAs2 fileName:="\FILE" & oDoc.Name & ".txt", _
                               FileFormat:=wdFormatText, _
                               LockComments:=False, _
                               Password:="", _
                               AddToRecentFiles:=True, _
                               WritePassword:="", _
                               ReadOnlyRecommended:=False, _
                               EmbedTrueTypeFonts:=False, _
                               SaveNativePictureFormat:=False, _
                               SaveFormsData:=False, _
                               SaveAsAOCELetter:=False, _
                               Encoding:=1252, _
                               InsertLineBreaks:=True, _
                               AllowSubstitutions:=False, _
                               LineEnding:=wdCRLF, _
                               CompatibilityMode:=0

        oDoc.Close SaveChanges:=False
        vFile = Dir
    Loop
End Sub

顺便说一句,您确定要使用 *.* 通配符吗?如果文件夹中有 Autocad 文件怎么办?另外 ActiveDocument.Name 会给你带扩展名的文件名.

BTW, are you sure you want to use the *.* wildcard? What if there are Autocad files in the folder? Also ActiveDocument.Name will give you the file name with the Extension.