我有一个宏,它接收一个Word文档,将数据复制到参数中,然后将其粘贴到多个单独的文档中(在本例中为三个)。
这是第一次使用VBA,因此请放轻松。
原始文档是一个长文档,其中包含多个重复部分。通过填写原始文档,用户可以节省完成一份而不是三份几乎相同的文档的时间。我将原件分为三个部分。我的代码从第一个声明的部分中获取数据,并将其粘贴到新文档中。它也适用于第三种。第二,但是不起作用。
The
With R.Find
.Text = "START OF FORM*^12"
.MatchWildcards = True
部分将查找文本“ Form of Start”,并将其和其余所有内容一直保留到“ ^ 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。我得到两个文件,一个包含第一部分(按预期),另一个包含最后两个文件。
答案 0 :(得分: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
如有任何疑问,请随时询问。