vba - 我们如何在 Word 文档的起点和终点之间进行复制?
问题描述
这是我正在测试的代码。
Sub CopySummary()
Dim lngStart As Long
Dim lngEnd As Long
Dim docNew As Document
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindStop
.MatchCase = False
.Text = "Start"
If .Execute = False Then
MsgBox "'Summary' not found.", vbExclamation
Exit Sub
End If
Selection.Collapse Direction:=wdCollapseEnd
lngStart = Selection.End
.Text = "End"
If .Execute = False Then
MsgBox "'End of Summary' not found.", vbExclamation
Exit Sub
End If
lngEnd = Selection.Start
End With
ActiveDocument.Range(lngStart, lngEnd).Copy
Set docNew = Documents.Add
docNew.Content.Paste
End Sub
这基本上有效,但问题是“开始”和“结束”在我的文档中出现了几次。此代码仅复制第一次出现的“开始”和“结束”。我想复制所有出现的“开始”和“结束”,以及“开始”和“结束”之间的所有文本。也许循环通过标题或循环通过段落,并在这个级别进行搜索?我对 Word 的 VBA 了解不多。
解决方案
This ended up working for me.
Sub ListPhrases()
Dim rng As Range
Dim strPhrases As String
Set rng = ActiveDocument.Content
With rng.Find
.ClearFormatting
.MatchWildcards = True
.Wrap = wdFindStop
.Text = "\Start*\End"
Do While .Execute
strPhrases = strPhrases & vbCr & rng.Text
Loop
End With
ActiveDocument.Content.InsertAfter strPhrases
End Sub
推荐阅读
- maven - jenkins中的maven构建因子模块错误而失败
- python - 如何使用 Python 拆分数据?
- vue.js - Vue-router 在切换 rutes 时一次渲染两个组件
- php - 接受带有条件的 URL 路径的正则表达式
- node.js - Mongoose/MongoDB 增加了两倍,我不知道为什么
- android - java.lang.IllegalStateException:Facebook 提供程序未配置
- typescript - 无法使用 Chrome DevTools 调试 Typescript 文件 - 未“链接到源映射”
- html - 如果网页没有移动版本,我应该设置哪个元视口标签?
- audio - 通过css或js隐藏html5音频标签上的volumeControl
- yii - 试图找到一个不存在 Yii2 的文件