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

时间:2017-12-25 19:52:09

标签: vba ms-word word-vba

我使用自定义分隔符将单个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

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分: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