English 中文(简体)
前面标题的文字文字用字字字表示
原标题:Text of the preceding heading in word
  • 时间:2012-05-24 16:17:35
  •  标签:
  • vba
  • ms-word

如果在单词中选中任何单词或段落, 是否有方法使用 VBA 查找最近的前一条标题的文本?

例如:

Heading Level 1: The Main Title This is a paragraph about the document. (A) Heading Level 2: A Sub Title This paragraph describes a detail.(B)

如果选中( B) 的任何部分, 我想找到“ A 子标题 ” 。 如果选中( A) 的任何部分, 我想找到“ 主标题 ” 。

问题回答

在上一个标题上有一个特殊的 < code> WdGoToemit :

Dim heading As Range
Set heading = selection.GoTo(What:=wdGoToHeading, Which:=wdGoToPrevious)

  Display heading text
heading.Expand Unit:=wdParagraph
MsgBox heading.Text

以下是一个微小的伎俩, 从文档的任何地方获取整个当前标题级别 :

Dim headingLevel as Range
  headingLevel encompasses the region under the preceding heading
Set headingLevel = Selection.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")

这就是你在尝试的吗?

Option Explicit

Sub Sample()
    Do
        Selection.MoveUp Unit:=wdLine, Count:=1

        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend

        If ActiveDocument.ActiveWindow.Selection.Information(wdFirstCharacterLineNumber) = 1 Then Exit Do
    Loop Until Selection.Style <> "Normal"

    MsgBox Selection.Style
End Sub

< 坚固 > SANPSHOT

""https://i.sstatic.net/2RluP.png" alt="此处输入图像描述"/ >





相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签