使用宏将Word文档拆分为多个.txt文件

问题描述 投票:-2回答:1

我使用自定义分隔符将单个MS Word文档拆分为多个。我能够以MS Word格式创建多个文件,但我想创建多个.txt文件。

我现在使用的代码是:

Sub SplitNotes(delim As String, strFilename As String)
    Dim doc As Document
    Dim arrNotes
    Dim I As Long
    Dim X As Long
    Dim Response As Integer

    arrNotes = Split(ActiveDocument.Range, delim)

    Response = MsgBox("This will split the document into " & 
UBound(arrNotes) + 1 & " sections. Do you wish to proceed?", 4)

    If Response = 7 Then Exit Sub

    For I = LBound(arrNotes) To UBound(arrNotes)

        If Trim(arrNotes(I)) <> "" Then

            X = X + 1

            Set doc = Documents.Add
            doc.Range = arrNotes(I)

            doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "000")
            doc.Close True

        End If

    Next I

End Sub


Sub test()
     '      delimiter & filename
    SplitNotes "%%%%%%%%%%%%%%", "Notes "
End Sub

有人可以帮我这个吗?

vba ms-word word-vba
1个回答
0
投票

试试这个,看看它是否符合你的要求。

Sub SplitNotes(delim As String, strFilename As String)
    Dim doc As Document
    Dim arrNotes
    Dim I As Long
    Dim X As Long
    Dim Response As Integer

    arrNotes = Split(ActiveDocument.Range, delim)

    Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections. Do you wish to proceed?", 4)
    If Response = 7 Then Exit Sub
    For I = LBound(arrNotes) To UBound(arrNotes)
        If Trim(arrNotes(I)) <> "" Then
            X = X + 1
            Set doc = Documents.Add
            doc.Range = arrNotes(I)
            doc.SaveAs ThisDocument.Path & "\" & strFilename & ".txt"
            doc.Close True
        End If
    Next I
End Sub


Sub test()
     ' delimiter & filename
    SplitNotes "///", "Notes "
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.