我有以下Word 2019宏代码:
`子 CreateBibleIndex() Dim bibleRefs() 作为字符串 Dim indexEntries() 作为字符串 调暗我一样长,j一样长
'正则表达式模式以匹配圣经参考 Dim regexPattern 作为字符串 regexPattern = "([1-3]?\s?[A-Z][a-z]*.)(\s\d{1,3}[:]\d{1,3}(-\d{1,3 })?)?"
' 圣经书卷的规范清单 将 bookList() 调暗为字符串 bookList = Split("Genesis,Exodus,Leviticus,Numbers,Deuteronomy,Joshua,Judges,Ruth,1 Samuel,2 Samuel,1 Kings,2 Kings,1 Chronicles,2 Chronicles,Ezra,Nehemiah,Esther,Job,Psalms,Proverbs ,传道书,雅歌,以赛亚书,耶利米书,耶利米哀歌,以西结书,但以理书,何西阿书,约珥书,阿摩司书,俄巴底亚书,约拿书,弥迦书,那鸿书,哈巴谷书,西番雅书,哈该书,撒迦利亚书,玛拉基书,马太福音,马可福音,路加福音,约翰福音,使徒行传,罗马书,1 哥林多前书,2 哥林多前书,加拉太书,以弗所书,腓立比书,歌罗西书,1 帖撒罗尼迦人,2 帖撒罗尼迦人,1 提摩太书,2 提摩太书,提多书,腓利门书,希伯来书,雅各书,1 彼得书,2 彼得书,1 约翰书,2 约翰书,3约翰,裘德,启示录”,“,”)
' 在文档中查找所有圣经参考资料 使用 ActiveDocument.range.Find .ClearFormating .MatchWildcards = 真 .Text = 正则表达式模式 。执行 而.Found ReDim 保留 bibleRefs(i) bibleRefs(i) = .文本 我 = 我 + 1 。执行 温德 结束于
' 遍历圣经参考资料并创建索引条目 对于 i = LBound(bibleRefs) 到 UBound(bibleRefs) 昏暗参考作为字符串 ref = bibleRefs(i) 昏暗的书作为字符串 昏暗的章节作为字符串 昏暗的诗句作为字符串 book = Left(ref, InStr(ref, " ") - 1) chapter = Mid(ref, InStr(ref, " ") + 1, InStr(ref, ":") - InStr(ref, " ") - 1) verse = Mid(ref, InStr(ref, ":") + 1) Dim indexEntry 作为字符串 indexEntry = book & " " & chapter & ":" & verse 如果不是 IsInArray(indexEntry, indexEntries) 那么 ReDim 保留 indexEntries(j) indexEntries(j) = indexEntry j = j + 1 万一 接下来我
' 按规范顺序对索引条目进行排序 Dim sortedEntries() 作为字符串 sortedEntries = SortIndexEntries(indexEntries,bookList)
' 在文档中创建索引 将 indexText 调暗为字符串 indexText = "索引" & vbCrLf 对于 i = LBound(sortedEntries) 到 UBound(sortedEntries) indexText = indexText & sortedEntries(i) & vbTab & "p. " & GetPageNumber(sortedEntries(i)) & vbCrLf 接下来我 ActiveDocument.range.InsertAfter indexText 结束子
函数 IsInArray(stringToBeFound As String, arr As Variant) As Boolean
` 我收到标题中的错误。看不到如何修复。谁能帮忙?谢谢
萨萨慈悲: 亲爱的,我尝试运行以下代码,“.Execute”行给出错误 5560“[Find target] contains an invalid [pattern comparison search] statement.”错误。问 YouChat 和 Bing,答案是“.Text = regexPattern”这一行是无效的:
为我之前回复中的错误道歉。的确, Word VBA 中查找对象的文本属性不支持正则 表达式。相反,您可以使用 VBScript RegExp 对象来 执行正则表达式搜索。
下面是如何在 Word 中使用 VBScript RegExp 对象的示例 VBA.
Dim regex As Object
Set regex = CreateObject("vbscript.regexp")
regex.Global = True
regex.Pattern = "the[A-Z]{2,6}\s"
Dim match As Object
For Each match In regex.Execute(ActiveDocument.Content.Text)
' do something with each match
Next match
在这段代码中,我们首先使用 CreateObject 创建一个 RegExp 对象 功能。然后我们将对象的 Global 属性设置为 True,这 允许它匹配 Pattern 属性设置为常规 我们要搜索的表达模式。
然后我们使用 RegExp 对象的 Execute 方法来执行 搜索活动文档的内容。结果是 Match 对象的集合,我们可以循环执行某些操作 每场比赛。
如果您还有其他问题,请告诉我。
冰说:
Word VBA的Find对象的Text属性不支持正则 表达式。但是,它确实支持使用通用搜索 人物。可以用特殊的字符组合来表示 特定的搜索模式,例如“?”可以匹配任何一个字符, 而“*”可以匹配零个或多个字符。
如果你想在Word VBA中使用正则表达式,你可以使用 VBScript 的 RegExp 对象。此对象提供对常规的支持 表达式并允许您对文本使用正则表达式 在 Word VBA 中匹配。
希望我的回答能帮到您!
Option Explicit
Sub CreateBibleIndex()
Dim bibleRefs() As String
Dim indexEntries() As String
Dim i As Long, j As Long
' Regular expression pattern to match Bible references
Dim regexPattern As String
regexPattern = "([1-3]?\s?[A-Z][a-z]*.)(\s\d{1,3}[:]\d{1,3}(-\d{1,3})?)?"
' Canonical list of books of the Bible
Dim bookList() As String
bookList = Split("Genesis,Exodus,Leviticus,Numbers,Deuteronomy,Joshua,Judges,Ruth,1 Samuel,2 Samuel,1 Kings,2 Kings,1 Chronicles,2 Chronicles,Ezra,Nehemiah,Esther,Job,Psalms,Proverbs,Ecclesiastes,Song of Solomon,Isaiah,Jeremiah,Lamentations,Ezekiel,Daniel,Hosea,Joel,Amos,Obadiah,Jonah,Micah,Nahum,Habakkuk,Zephaniah,Haggai,Zechariah,Malachi,Matthew,Mark,Luke,John,Acts,Romans,1 Corinthians,2 Corinthians,Galatians,Ephesians,Philippians,Colossians,1 Thessalonians,2 Thessalonians,1 Timothy,2 Timothy,Titus,Philemon,Hebrews,James,1 Peter,2 Peter,1 John,2 John,3 John,Jude,Revelation", ",")
' Find all Bible references in the document
With ActiveDocument.Range.Find
.ClearFormatting
.MatchWildcards = True
.Text = regexPattern
.Execute
While .Found
ReDim Preserve bibleRefs(i)
bibleRefs(i) = .Text
i = i + 1
.Execute
Wend
End With
' Loop through Bible references and create index entries
For i = LBound(bibleRefs) To UBound(bibleRefs)
Dim ref As String
ref = bibleRefs(i)
Dim book As String
Dim chapter As String
Dim verse As String
book = Left(ref, InStr(ref, " ") - 1)
chapter = Mid(ref, InStr(ref, " ") + 1, InStr(ref, ":") - InStr(ref, " ") - 1)
verse = Mid(ref, InStr(ref, ":") + 1)
Dim indexEntry As String
indexEntry = book & " " & chapter & ":" & verse
If Not IsInArray(indexEntry, indexEntries) Then
ReDim Preserve indexEntries(j)
indexEntries(j) = indexEntry
j = j + 1
End If
Next i
' Sort index entries in canonical order
Dim sortedEntries() As String
sortedEntries = SortIndexEntries(indexEntries, bookList)
' Create index in document
Dim indexText As String
indexText = "Index" & vbCrLf
For i = LBound(sortedEntries) To UBound(sortedEntries)
indexText = indexText & sortedEntries(i) & vbTab & "p. " & GetPageNumber(sortedEntries(i)) & vbCrLf
Next i
ActiveDocument.Range.InsertAfter indexText
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
End Function
Function SortIndexEntries(indexEntries() As String, bookList() As String)
End Function
Function GetPageNumber(sortedEntries) As String
End Function
如果您收到标题中显示的“子或函数未定义”之类的错误(
我得到标题中的错误。……
), 看看你在“CreateBibleIndex”子程序中调用的“SortIndexEntries”方法是否正确实现了
赞美主阿门
感恩感恩 南无阿弥陀佛