我有一长串word文档,它们都有三页。现在我想要文档 1 中的每个第一页、文档 2 中的每个第二页和文档 3 中的每个第三页。我的 word 文档中的每一页都有标签,但每一页都有相同的标签。我需要搜索标签,选择标签和介于两者之间的所有内容,然后将它们移动到新文档中。然后,再次搜索以找到第二个标签(与第一个标签相同的文本)并执行相同的操作。
我有一个带有标签的所有文档的文件名/位置的 excel 表,所以我从 excel vba 运行所有这些。
我已尝试查找/选择代码,但它选择了第一个和最后一个标签,而不是第一个标签。你能帮帮我吗?
这是我当前用于一一打开单词文档并查找标签的代码:
Sub SelectRangeBetween()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") 'Change to the correct sheetname
Dim wrdApp As Word.Application
' Set wrdApp = CreateObject("Word.Application")
Dim WrdDoc As Word.Document
Set wrdApp = New Word.Application '
wrdApp.Visible = True 'set to false for higher speed
Const StarttagColumn = "C" 'Edit this for the column of the starttag.
Const EndtagColumn = "D" 'Edit this for the column of the endtag.
Const FilelocationColumn = "E" 'Edit this for the column of the Filelocation.
Const startRow As Long = 5 'This is the first row of tags and filenames
'Const endRow As Long = 140 'uncomment if you want a fixed amount of rows (for ranges with empty cells)
Dim endRow As Long 'comment out if const-endrow is used
endRow = ws.Range("B" & Rows.Count).End(xlUp).Row 'comment out if const-endrow is used
Dim i As Long
For i = startRow To endRow
Dim wrdPath As String
wrdPath = ws.Cells(i, FilelocationColumn).Value2 '
If wrdPath <> vbNullString Then '
If Dir(wrdPath) <> vbNullString Then '
Dim startTag As String '
Dim endTag As String '
startTag = ws.Cells(i, StarttagColumn).Value2 '
endTag = ws.Cells(i, EndtagColumn).Value2 '
Set WrdDoc = wrdApp.Documents.Open(wrdPath) '
With wrdApp
With .ActiveDocument.Content.Duplicate
.Find.Execute Findtext:=startTag & "*" & endTag, MatchWildcards:=False, Forward:=False
.MoveStart wdCharacter, Len(startTag)
.MoveEnd wdCharacter, -Len(endTag) - 1
.Select ' Or whatever you want to do
End With
End With
With WrdDoc
.Close
End With
End If
End If
Next i
End Sub