Word VBA:在文档正文中查找、复制关键短语并将其粘贴到页面顶部的书签位置
Word VBA: Find, copy and paste key phrases within document body to a bookmarked location at the top of page
我需要找到一个解决方案来创建一个“报告摘要”子项,该子项在 word 文档中找到各种结果数据的顶行,并将它们插入到页面顶部的书签位置下.
每个结果作为一个共同的值才能找到,扩展到select整行然后复制。我正在努力解决的问题是找到所有符合条件的短语并将它们粘贴到页面顶部的书签位置。我可以让 sub 复制该短语的第一个实例,但不是所有实例。对于上下文,我使用的关键短语是“:”,因为所有结果都包含此值。我尝试的另一种方法导致子创建无限循环,粘贴短语的第一个实例,直到 Word 崩溃。我想我需要编写一个脚本来找到该短语,然后在复制和粘贴后移动到下一个实例,最后在到达文档末尾时结束子。以下是我目前的立场:
Sub Report_SummaryTest()
If Selection.StoryType <> wdMainTextStory Then
With ActiveDocument.ActiveWindow.View
.Type = wdPrintView
.SeekView = wdSeekMainDocument
End With
End If
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = " : "
.Format = False
.Forward = True
.MatchWildcards = False
.Wrap = wdFindStop
While .Execute
Selection.Expand Unit:=wdLine
Selection.Copy ' Unit:=wdLine
Selection.GoTo What:=wdGoToBookmark, Name:="RepSummary"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Paste
Wend
End With
End Sub
结果数据(每个报告 7 或 8 个以上)如下所示:
测试结果一:阳性
指南:
结果一的准则。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指南。
理想情况下,我希望“报告摘要”看起来像这样:
摘要
测试结果一:阳性
测试结果二:阳性
测试结果三:阴性
非常感谢任何建议。
您可以简单地使用 Table 的内容
我不知道你为什么要重新发明轮子。您可以在特定样式中包含您想要的内容或使用 TC 字段。 Word 中的 Table 目录是目录字段。您不需要页码并且可以真正自定义它。这是 Microsoft page on this field.
Syntax
When you view the TOC field in your document, the syntax looks like
this:
{ TOC [Switches ] }
Switches that determine what's included in the table of contents
\o "Levels"
Builds a table of contents from paragraphs formatted with styles that
include outline levels (most commonly, heading styles). For example, {
TOC \o "1-3" } lists only paragraphs formatted with styles that
include outline levels 1 through 3. If no range of outline levels is
specified, all outline levels used in the document are listed. Enclose
the range numbers in quotation marks.
\t "Style,Level,Style,Level,..."
Builds a table of contents from paragraphs formatted with styles other
than the built-in styles. For example, { TOC \t "chaptertitle,1,
chapterhead,2" } builds a table of contents from paragraphs formatted
with the styles "chaptertitle" and "chapterhead." The number after
each style name indicates the table of contents entry level that
corresponds to that style.
You can use both the \o switch and the \t switch to build a table of
contents from built-in styles and custom styles.
\u
Builds a table of contents from paragraphs whose formatting includes
outline levels applied directly, in paragraph settings.
\c "SEQIdentifier"
Lists figures, tables, charts, or other items that are numbered by a
SEQ (Sequence) field. Word uses SEQ fields to number items captioned
with the Caption command (References > Insert Caption). SEQIdentifier,
which corresponds to the caption label, must match the identifier in
the SEQ field. For example, { TOC \c "tables" } lists all numbered
tables.
\a Identifier
Lists items captioned with the Caption command (References > Insert
Caption) but omits caption labels and numbers. The identifier
corresponds to the caption label. For example, although a caption on
page 12 is "Figure 8: Mercury", the field { TOC \a figures } displays
entries as "Mercury............12".
Use the \c switch to build a table of captions with labels and
numbers.
\f EntryIdentifier
Builds a table from TC fields. If EntryIdentifier is specified, the
table is built only from TC fields with the same identifier (typically
a letter). For example, { TOC \f t } builds a table of contents from
TC fields such as { TC "Entry Text" \f t }.
\l Levels
Builds a table of contents from TC fields that assign entries to one
of the specified levels. For example, { TOC \l 1-4 } builds a table of
contents from TC fields that assign entries to levels 1-4. TC fields
that assign entries to lower levels are skipped.
\b BookmarkName
Collects entries only from the portion of the document marked by the
specified bookmark.
Switches that format the page number
\s Identifier
Includes a number such as a chapter number before the page number. The
chapter or other item must be numbered with a SEQ field. Identifier
must match the identifier in the SEQ field. For example, if you insert
{ SEQ chapter } before each chapter heading, { TOC \o "1-3" \s chapter
} displays page numbers as 2-14, where "2" is the chapter number.
\d "Separator"
When used with the \s switch, specifies the character that separates
the sequence numbers and page numbers. Enclose the character in
quotation marks. Word uses a hyphen (-) if no \d switch is specified.
In the table of contents generated by { TOC \o "1-3" \s chapter \d ":"
}, a colon (:) separates chapter numbers and page numbers — for
example, "2:14."
\p "Separator"
Specifies the character that separates an entry and its page number.
For example, the field { TOC \p "—" }, with an em dash, displays a
result such as "Selecting Text—53." The default is a tab with leader
dots. Just one character is allowed; it must be enclosed in quotation
marks.
\n Levels
Omits page numbers from the table of contents. Page numbers are
omitted from all levels unless a range of entry levels is specified.
For example, { TOC \n 3-4 } omits page numbers from levels 3 and 4.
Delete this switch to include page numbers.
还有很多没有在这里引用。
有关这方面的更多信息,请参阅 Word MVP Suzanne Barnhill 的 TOC Tricks and Tips。
我需要找到一个解决方案来创建一个“报告摘要”子项,该子项在 word 文档中找到各种结果数据的顶行,并将它们插入到页面顶部的书签位置下.
每个结果作为一个共同的值才能找到,扩展到select整行然后复制。我正在努力解决的问题是找到所有符合条件的短语并将它们粘贴到页面顶部的书签位置。我可以让 sub 复制该短语的第一个实例,但不是所有实例。对于上下文,我使用的关键短语是“:”,因为所有结果都包含此值。我尝试的另一种方法导致子创建无限循环,粘贴短语的第一个实例,直到 Word 崩溃。我想我需要编写一个脚本来找到该短语,然后在复制和粘贴后移动到下一个实例,最后在到达文档末尾时结束子。以下是我目前的立场:
Sub Report_SummaryTest()
If Selection.StoryType <> wdMainTextStory Then
With ActiveDocument.ActiveWindow.View
.Type = wdPrintView
.SeekView = wdSeekMainDocument
End With
End If
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = " : "
.Format = False
.Forward = True
.MatchWildcards = False
.Wrap = wdFindStop
While .Execute
Selection.Expand Unit:=wdLine
Selection.Copy ' Unit:=wdLine
Selection.GoTo What:=wdGoToBookmark, Name:="RepSummary"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Paste
Wend
End With
End Sub
结果数据(每个报告 7 或 8 个以上)如下所示:
测试结果一:阳性
指南:
结果一的准则。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指导方针。结果一的指南。
理想情况下,我希望“报告摘要”看起来像这样:
摘要
测试结果一:阳性
测试结果二:阳性
测试结果三:阴性
非常感谢任何建议。
您可以简单地使用 Table 的内容
我不知道你为什么要重新发明轮子。您可以在特定样式中包含您想要的内容或使用 TC 字段。 Word 中的 Table 目录是目录字段。您不需要页码并且可以真正自定义它。这是 Microsoft page on this field.
Syntax
When you view the TOC field in your document, the syntax looks like this:
{ TOC [Switches ] }
Switches that determine what's included in the table of contents
\o "Levels"
Builds a table of contents from paragraphs formatted with styles that include outline levels (most commonly, heading styles). For example, { TOC \o "1-3" } lists only paragraphs formatted with styles that include outline levels 1 through 3. If no range of outline levels is specified, all outline levels used in the document are listed. Enclose the range numbers in quotation marks.
\t "Style,Level,Style,Level,..."
Builds a table of contents from paragraphs formatted with styles other than the built-in styles. For example, { TOC \t "chaptertitle,1, chapterhead,2" } builds a table of contents from paragraphs formatted with the styles "chaptertitle" and "chapterhead." The number after each style name indicates the table of contents entry level that corresponds to that style.
You can use both the \o switch and the \t switch to build a table of contents from built-in styles and custom styles.
\u
Builds a table of contents from paragraphs whose formatting includes outline levels applied directly, in paragraph settings.
\c "SEQIdentifier"
Lists figures, tables, charts, or other items that are numbered by a SEQ (Sequence) field. Word uses SEQ fields to number items captioned with the Caption command (References > Insert Caption). SEQIdentifier, which corresponds to the caption label, must match the identifier in the SEQ field. For example, { TOC \c "tables" } lists all numbered tables.
\a Identifier
Lists items captioned with the Caption command (References > Insert Caption) but omits caption labels and numbers. The identifier corresponds to the caption label. For example, although a caption on page 12 is "Figure 8: Mercury", the field { TOC \a figures } displays entries as "Mercury............12".
Use the \c switch to build a table of captions with labels and numbers.
\f EntryIdentifier
Builds a table from TC fields. If EntryIdentifier is specified, the table is built only from TC fields with the same identifier (typically a letter). For example, { TOC \f t } builds a table of contents from TC fields such as { TC "Entry Text" \f t }.
\l Levels
Builds a table of contents from TC fields that assign entries to one of the specified levels. For example, { TOC \l 1-4 } builds a table of contents from TC fields that assign entries to levels 1-4. TC fields that assign entries to lower levels are skipped.
\b BookmarkName
Collects entries only from the portion of the document marked by the specified bookmark.
Switches that format the page number
\s Identifier
Includes a number such as a chapter number before the page number. The chapter or other item must be numbered with a SEQ field. Identifier must match the identifier in the SEQ field. For example, if you insert { SEQ chapter } before each chapter heading, { TOC \o "1-3" \s chapter } displays page numbers as 2-14, where "2" is the chapter number.
\d "Separator"
When used with the \s switch, specifies the character that separates the sequence numbers and page numbers. Enclose the character in quotation marks. Word uses a hyphen (-) if no \d switch is specified. In the table of contents generated by { TOC \o "1-3" \s chapter \d ":" }, a colon (:) separates chapter numbers and page numbers — for example, "2:14."
\p "Separator"
Specifies the character that separates an entry and its page number. For example, the field { TOC \p "—" }, with an em dash, displays a result such as "Selecting Text—53." The default is a tab with leader dots. Just one character is allowed; it must be enclosed in quotation marks.
\n Levels
Omits page numbers from the table of contents. Page numbers are omitted from all levels unless a range of entry levels is specified. For example, { TOC \n 3-4 } omits page numbers from levels 3 and 4. Delete this switch to include page numbers.
还有很多没有在这里引用。
有关这方面的更多信息,请参阅 Word MVP Suzanne Barnhill 的 TOC Tricks and Tips。