为什么我的VBA宏只分割Word文档的第1和第3部分?

问题描述 投票:0回答:1

我有一个宏,它接受一个Word文档,复制我的参数内的数据,然后粘贴多个单独的文件(在这种情况下三个)。

这是第一次使用VBA,所以请放心。

原始文档是一个长文档,它有多个重复部分。通过填写原始文档,用户可以节省完成一个而不是三个几乎相同的文档的时间。我将原文分为三个部分。我的代码从第一个声明的部分获取数据并将其粘贴到新文档中。它也适用于第三个。然而,第二个不起作用。

With R.Find
.Text = "START OF FORM*^12"
.MatchWildcards = True

部分查找文本“表单的开始”并将其余内容直到'^ 12'(我相信是指分页符)。

设置文档,以便文档的每个部分都以该文本开头,并以分页符结束。

Sub DocSplit()

' Declares variable (in this case R).
Dim R As Range

' Sets R to the active document, being a number of ranges (will be defined later).
Set R = ActiveDocument.Range.Duplicate

'  You won't be able to see what the macro is doing, but will run quicker.
Application.ScreenUpdating = False

' For R, find text with whatever is in the " marks.
With R.Find
.Text = "START OF FORM*^12"
.MatchWildcards = True

' Runs a series of statements as long as given conditions are true. While it's doing this,
While .Execute

' Copy and saves contents of R.
CopyAndSave R

' While ends.
Wend

'With ends.
End With

' Collapses range to the ending point.
R.Collapse wdCollapseEnd

' Returns or sets the ending character position of a range.
R.End = R.Parent.Range.End
CopyAndSave R

End Sub
Static Sub CopyAndSave(R As Range)

' Declares D as document.
Dim D As Document

' Represents the number of words in the collection.
' Long is a datatype for values too large for "integer".
Dim Count As Long
Count = Count + 1

' Copies R from previous Sub to a new document.
R.Copy
Set D = Documents.Add

' Pastes range, preserving original formatting.
D.Range.PasteAndFormat wdFormatOriginalFormatting


D.SaveAs R.Parent.Path & Application.PathSeparator & _
"F00" & Count, wdFormatDocument
D.Close

End Sub

我确实希望创建三个文件,F001,F002和F003。我得到两个文件,一个包含第一部分(按预期)和一个包含最后两个文件的文件。

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

我快速查看了您的代码,发现了以下错误:

  • 如果你想在每次调用函数时增加counter,你必须在main函数中声明它,否则每次调用它都会丢失内存。
  • R.Find需要一个论点。如果您想了解更多细节,请查看here
  • R.End需要一个论点,here你会找到一些提示,这取决于你需要做什么。

我已更新代码的某些部分以帮助您:

Sub DocSplit()

    ' Declares variable (in this case R).
    Dim R As Range

    ' Represents the number of words in the collection.
    ' Long is a datatype for values too large for "integer".
    Dim count As Long
    count = 0

    ' Sets R to the active document, being a number of ranges (will be defined later).
    Set R = ActiveDocument.Range.Duplicate

    '  You won't be able to see what the macro is doing, but will run quicker.
    Application.ScreenUpdating = False

    ' For R, find text with whatever is in the " marks.
    With R.Find("Text your're searching")
        .Text = "START OF FORM*^12"
        .MatchWildcards = True

        ' Runs a series of statements as long as given conditions are true. While it's doing this,
        While .Execute

            ' Copy and saves contents of R.
            Call CopyAndSave(R, count)

        ' While ends.
        Wend

    'With ends.
    End With

    ' Collapses range to the ending point.
    R.Collapse wdCollapseEnd

    ' Returns or sets the ending character position of a range.
    R.End = R.Parent.Range.End
    Call CopyAndSave(R)

End Sub
Static Sub CopyAndSave(R As Range, count As Long)
    ' Declares D as document.
    Dim D As Document

    count = count + 1

    ' Copies R from previous Sub to a new document.
    R.Copy
    Set D = Documents.Add

    ' Pastes range, preserving original formatting.
    D.Range.PasteAndFormat wdFormatOriginalFormatting


    D.SaveAs R.Parent.Path & Application.PathSeparator & _
    "F00" & count, wdFormatDocument
    D.Close

End Sub

如果您有任何疑问,请不要犹豫。

© www.soinside.com 2019 - 2024. All rights reserved.