I want to change all instances of text using the Emphasis character style to the Strong Emphasis character style. I see a choice in the Find and Replace dialog to change Paragraph Styles. Is there a way to change Character Styles?
IMHO not directly, but as a find-all leaves all found places selected you may use this to assign a character style to all of them.
.
As a PS: Search will also find direct formatting, so it is not limited to paragraph styles.
1 Like
Perhaps something like this will help you?
Sub ReplaceCharStyle()
Const UndoName = "Replace CharStyles"
Const SearchStyleName = "Emphasis"
Const RelaceWithStyle = "Strong Emphasis"
Dim oText As Variant, oUndoManager As Variant
Dim oParagraphs As Variant, oParagraph As Variant
Dim oWords As Variant, oWord As Variant
oUndoManager = ThisComponent.getUndoManager()
oUndoManager.enterUndoContext(UndoName)
oText = ThisComponent.getText()
oParagraphs = oText.createEnumeration()
While oParagraphs.hasMoreElements()
oParagraph = oParagraphs.nextElement()
oWords = oParagraph.createEnumeration()
While oWords.hasMoreElements()
oWord = oWords.nextElement()
If oWord.CharStyleName = SearchStyleName Then oWord.CharStyleName = RelaceWithStyle
Wend
Wend
oUndoManager.leaveUndoContext()
End Sub
2 Likes
@JohnSUN
the for each … command can also manage the enumeration-access
Sub ReplaceCharStyle()
Const UndoName = "Replace CharStyles"
Const SearchStyle = "Emphasis"
Const ReplaceStyle = "Strong Emphasis"
oUndoManager = ThisComponent.getUndoManager()
oUndoManager.enterUndoContext(UndoName)
oText = ThisComponent.getText()
for each paragraph in oText
for each part in paragraph
if part.CharStyleName = SearchStyle then
part.CharStyleName = ReplaceStyle
end if
next part
next paragraph
oUndoManager.leaveUndoContext()
End Sub