Windows 7、Word 2010 和 2016
我有一个宏,它可以在我的文档中搜索一些文本,找到后就将该文本添加到索引中。我的想法是,我有很多定义(格式:)[word] - means [definition]
,我想将所有这些定义都放入索引中,这样我就可以有效地在最后得到一本字典。
但是,当它运行并创建索引时,大约 90% 的条目是按字母顺序排列的,但有些条目的位置是随机的。据我所知,它们没有理由排错顺序。(在我的“A”字部分中,有一个单词以“C”或其他非“A”开头)。
这是将它们添加到索引的代码片段(我把它从一个更大的宏中提取出来,但如果您想要整个内容,请告诉我):
myDoc.Indexes.MarkEntry Range:=rng, entry:=editedDefinition, entryautotext:=editedDefinition
myDoc
是Word.Document
( myDoc = ActiveDocument
)。
rng
是Word.Range
editedDefinition
是String
。
我感觉我添加到索引的那行太简单了。是否需要更多明确的信息?
此外,不管怎样,索引似乎只允许您添加最多几个字符(由于某种原因,它会切断一些定义)。
编辑:这是主要的宏(您会注意到我调用了一个 UDF,如果您也需要它,请告诉我):
Sub Find_Definitions()
Dim myDoc As Word.Document
Dim oRng As Word.Range, rng As Word.Range
Dim addDefinition$, findText$, editedDefinition$
Set myDoc = ActiveDocument
Call Clear_Index
findText = InputBox("What term would you like to search for?")
If findText = "" Then Exit Sub
'Loop through the document
Set oRng = myDoc.Content
With oRng.Find
.ClearFormatting
.Text = findText
.MatchCase = False
.Wrap = wdFindStop
While .Execute
Set rng = oRng.Paragraphs(1).Range
rng.Select
Dim searchText$
searchText = "- Non- USA"
If Left(rng.Text, Len(searchText)) = searchText Then
Debug.Print ""
End If
' Here's where I could check the text, and see if it starts with Roman numerals.
editedDefinition = Check_For_Roman_Numerals(rng, findText)
' Check to see if we're in the 'Definitions' section
If rng.Information(wdActiveEndSectionNumber) >= myDoc.Sections.Count - 1 Then
GoTo TheEnd
End If
myDoc.Indexes.MarkEntry Range:=rng, entry:=editedDefinition, entryautotext:=editedDefinition
Wend 'end .execute
End With 'oRng.find
TheEnd:
Set rng = Nothing
myDoc.Indexes(1).Update
MsgBox ("Added all definitions.")
End Sub
编辑:(根据评论)我想我找到了这个问题!在网上搜索后,我发现这个帖子这似乎是我的问题!我做了一个测试,从其中一个无序条目中删除了一个分号,然后它把它放在了正确的位置。现在,我只需要弄清楚如何在添加到索引时考虑 ;。我对 Word VBA 还不熟悉,所以任何想法/提示都会很感激。
编辑2:这是我的 UDF:
Private Function Check_For_Roman_Numerals(ByVal mySelection As Word.Range, searchString As String) As String
Dim romanNumerals() As Variant
Dim firstWord$, paragraphsText As Variant, xWord As Variant
Dim oWord As Word.Range
Dim i&, x&
romanNumerals = Array("i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii")
Dim editedSelection
Dim moveStart As Variant
Dim myEditedSelection As Variant
Dim addedOnce As Boolean
'editedSelection = mySelection.Text
x = 0
addedOnce = False
With mySelection
Debug.Print mySelection.Text
' Edit selection to include only the start where it's underlined
On Error Resume Next
Do Until mySelection.Characters(x + 1).Font.Underline = wdUnderlineSingle Or mySelection.Characters(x + 1).Font.Underline = wdUnderlineDouble
If (x + 1) > mySelection.Characters.Count Then Exit Do
Debug.Print "'" & mySelection.Characters(x + 1) & "' is not underlined"
x = x + 1
Loop
On Error GoTo 0
Set myEditedSelection = mySelection.Duplicate '= mySelection.moveStart(unit:=wdWord, Count:=x)
With myEditedSelection
.moveStart unit:=wdCharacter, Count:=x
.Select
End With 'myEditedSelection
For i = LBound(romanNumerals) To UBound(romanNumerals)
If (mySelection.Words(1) = romanNumerals(i)) Or (mySelection.Words(1) = romanNumerals(i) & ".") Then
Debug.Print "Found roman numeral " & mySelection.Words(1)
moveStart = trim_Roman_Text(mySelection.Text, searchString, myEditedSelection.moveStart(unit:=wdCharacter, Count:=x) + 1)
editedSelection = moveStart
Debug.Print "Adding: """ & editedSelection & """ to Index"
Exit For
ElseIf Not addedOnce Then
moveStart = trim_Text(mySelection.Text, searchString, myEditedSelection.moveStart(unit:=wdCharacter, Count:=x) + 1)
editedSelection = Trim(moveStart)
addedOnce = True
End If
Next i
End With 'mySelection
Check_For_Roman_Numerals = editedSelection
End Function
Private Function trim_Text(ByVal myText As String, mySearch As String, startPos As Integer) As String
Dim finalText$
Dim sentenceEndPosition&, meansPos&
meansPos = InStr(1, myText, mySearch)
sentenceEndPosition = InStr(meansPos, myText, ".")
If sentenceEndPosition = 0 Then
sentenceEndPosition = InStr(meansPos, myText, ";")
End If
If sentenceEndPosition = 0 Then
sentenceEndPosition = InStr(meansPos, myText, ":")
End If
If sentenceEndPosition = 0 Then
sentenceEndPosition = InStr(meansPos, myText, Chr(13))
End If
If sentenceEndPosition = 0 Then
MsgBox ("What is the end of the paragraph?")
End If
finalText = Trim(Mid(myText, startPos, sentenceEndPosition))
trim_Text = finalText
End Function
Private Function trim_Roman_Text(ByVal myText As String, ByVal mySearch As String, startPos As Integer) As String
Dim finalText$
Dim romanNumeralEndPosition&, sentenceEndPosition$, meansPos&
'myText = "i. Australia - means the subcontinent. It is located below Asia, and this is what it looks like. A giant circle with some odd edges."
meansPos = InStr(1, myText, mySearch)
romanNumeralEndPosition = InStr(1, myText, ".")
'Debug.Print romanNumeralEndPosition
sentenceEndPosition = InStr(romanNumeralEndPosition + 1, myText, ".")
If sentenceEndPosition = 0 Then
sentenceEndPosition = InStr(romanNumeralEndPosition + 1, myText, ";")
End If
If sentenceEndPosition = 0 Then
sentenceEndPosition = InStr(romanNumeralEndPosition + 1, myText, ":")
End If
If sentenceEndPosition = 0 Then
sentenceEndPosition = InStr(romanNumeralEndPosition + 1, myText, Chr(13))
End If
'Debug.Print sentenceEndPosition
finalText = Trim(Mid(myText, romanNumeralEndPosition + 1, sentenceEndPosition - romanNumeralEndPosition))
'Debug.Print finalText
trim_Roman_Text = finalText
End Function
答案1
因此,索引条目标记中的分号似乎是一个用于覆盖排序的特殊字符。您的链接暗示这是一个错误,但它似乎是一个功能,并且它证实了个人在您的链接中叙述的行为。
冒号也是一个特殊字符,必须用反斜杠转义\
才能在文本中使用。据推测,您也可以转义分号。在软件和预处理器中,序列中的任何控制字符都可以转义,这是很常见的(但不保证)。
我无法快速找到有关逃脱的任何官方文档,但是此 PDF 文件有一个很好的概述。
内置排序 { XE "Alfonso IV" }
覆盖排序 { XE "阿方索四世;阿方索四世" }
索引条目文本中的特殊字符 如果您需要在索引条目文本中包含冒号,请在其前面加上反斜杠来“转义”它(例如“ Luke 9\:21” )。
如果要在索引条目文本中使用符号(例如 @),请在符号后立即输入“;#”(分号后跟数字符号)。
这来自微软的链接谈论分号,但没有关于转义冒号和分号的任何信息。