有人能指出这个宏有什么问题吗?我试图搜索带有标记“vv”的段落,然后将其段落样式更改为“_Verse”。如果我在其中放置一个断点并手动逐段执行,这个宏就可以工作。但是如果让它自己运行,它会使 CPU 核心达到最大限度,我必须强制降低 LO。
问题似乎是,
Loop While oEnum.hasMoreElements()
当到达文件末尾时,该行并没有继续。
谢谢。
sub findvv
rem define variables
dim document as object
dim dispatcher as object
oEnum = ThisComponent.Text.createEnumeration()
rem
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem
dim args1(21) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(3).Name = "SearchItem.AllTables"
args1(3).Value = false
args1(4).Name = "SearchItem.SearchFiltered"
args1(4).Value = false
args1(5).Name = "SearchItem.Backward"
args1(5).Value = false
args1(6).Name = "SearchItem.Pattern"
args1(6).Value = false
args1(7).Name = "SearchItem.Content"
args1(7).Value = false
args1(8).Name = "SearchItem.AsianOptions"
args1(8).Value = false
args1(9).Name = "SearchItem.AlgorithmType"
args1(9).Value = 1
args1(10).Name = "SearchItem.SearchFlags"
args1(10).Value = 65536
args1(11).Name = "SearchItem.SearchString"
args1(11).Value = "^vv"
args1(12).Name = "SearchItem.ReplaceString"
args1(12).Value = ""
args1(13).Name = "SearchItem.Locale"
args1(13).Value = 255
args1(14).Name = "SearchItem.ChangedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.DeletedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.InsertedChars"
args1(16).Value = 2
args1(17).Name = "SearchItem.TransliterateFlags"
args1(17).Value = 1073743104
args1(18).Name = "SearchItem.Command"
args1(18).Value = 0
args1(19).Name = "SearchItem.SearchFormatted"
args1(19).Value = false
args1(20).Name = "SearchItem.AlgorithmType2"
args1(20).Value = 2
args1(21).Name = "Quiet"
args1(21).Value = true
Do
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())
applyverse
Loop While oEnum.hasMoreElements()
end sub
sub applyverse
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Template"
args1(0).Value = "_Verse"
args1(1).Name = "Family"
args1(1).Value = 2
dispatcher.executeDispatch(document, ".uno:StyleApply", "", 0, args1())
end sub
答案1
在我看来,这您没有必要在一个代码中混合使用两种方法。要解决所述问题,其中一种方法就足够了。如果您已通过 获取所有文本段落.Text.createEnumeration()
,则只需使用 获取下一个段落oEnum.nextElement()
,使用 读取其文本值.getString()
并检查左侧两个字符以匹配速度图案
Sub setVvAsVerseEnum
Dim oText As Variant
Dim oEnum As Variant
Dim oPara As Variant
Dim sString As String
oText = ThisComponent.getText()
oEnum = oText.createEnumeration()
While oEnum.hasMoreElements()
oPara = oEnum.nextElement()
sString = oPara.getString()
If Left(sString,2) = "vv" Then oPara.ParaStyleName = "_Verse"
Wend
End Sub
如果要使用内置的正则表达式搜索,请创建一个搜索描述符,在其中指明搜索模式以及使用正则表达式进行搜索的需要。使用一个命令.FindAll()
,找到与模板相对应的所有段落,并为每个段落分配所需的段落样式
Sub setVvAsVerseByFindAll
Dim oSearchDescriptor As Variant
Dim oFound As Variant
Dim oPara As Variant
oSearchDescriptor = ThisComponent.createSearchDescriptor()
oSearchDescriptor.setSearchString("^vv.*$")
oSearchDescriptor.SearchRegularExpression = True
oFound = ThisComponent.FindAll(oSearchDescriptor)
For Each oPara In oFound
oPara.ParaStyleName = "_Verse"
Next oPara
End Sub
这两个代码的作用相同,并且都比宏录制器为您生成的代码短一些(而且我认为它更容易阅读,不是吗?)