How to search/replace by italic or bold in macro

I want to surround all bold text and italic text with deprecated HTML tags e.g. <b>text</b>. I can do this using the UI using a regex substitution from (.*) to <b>$1</b> (in the case of bold) and setting the Other options->Format… to select bold or italic as required.

If I record this action in a macro there is no difference between the commands to do italics tags and the commands to do bold tags i.e. the format filter isn’t reflected in the macro. It just acts on whatever format I last manually selected in the UI.

This is the code produced by Macro record. Apart from the SearchItem.ReplaceString everything in both operations is exactly the same. That’s the problem. I need to somehow specify whether the search acts on italic text or bold text.

rem Surround any bold or italics with deprecated HTML <i> and <b> tags
sub TagFormatting
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(20) 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 = "(.*)"
args1(12).Name = "SearchItem.ReplaceString"
args1(12).Value = "<i>$1</i>"
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 = 1280
args1(18).Name = "SearchItem.Command"
args1(18).Value = 3
args1(19).Name = "SearchItem.SearchFormatted"
args1(19).Value = false
args1(20).Name = "Quiet"
args1(20).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(20) as new com.sun.star.beans.PropertyValue
args2(0).Name = "SearchItem.StyleFamily"
args2(0).Value = 2
args2(1).Name = "SearchItem.CellType"
args2(1).Value = 0
args2(2).Name = "SearchItem.RowDirection"
args2(2).Value = true
args2(3).Name = "SearchItem.AllTables"
args2(3).Value = false
args2(4).Name = "SearchItem.SearchFiltered"
args2(4).Value = false
args2(5).Name = "SearchItem.Backward"
args2(5).Value = false
args2(6).Name = "SearchItem.Pattern"
args2(6).Value = false
args2(7).Name = "SearchItem.Content"
args2(7).Value = false
args2(8).Name = "SearchItem.AsianOptions"
args2(8).Value = false
args2(9).Name = "SearchItem.AlgorithmType"
args2(9).Value = 1
args2(10).Name = "SearchItem.SearchFlags"
args2(10).Value = 65536
args2(11).Name = "SearchItem.SearchString"
args2(11).Value = "(.*)"
args2(12).Name = "SearchItem.ReplaceString"
args2(12).Value ="<b>$1</b>"
args2(13).Name = "SearchItem.Locale"
args2(13).Value = 255
args2(14).Name = "SearchItem.ChangedChars"
args2(14).Value = 2
args2(15).Name = "SearchItem.DeletedChars"
args2(15).Value = 2
args2(16).Name = "SearchItem.InsertedChars"
args2(16).Value = 2
args2(17).Name = "SearchItem.TransliterateFlags"
args2(17).Value = 1280
args2(18).Name = "SearchItem.Command"
args2(18).Value = 3
args2(19).Name = "SearchItem.SearchFormatted"
args2(19).Value = false
args2(20).Name = "Quiet"
args2(20).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args2())

end sub

The macro recorder is very limited. It’s better to use UNO commands. For more help sign up at http://forum.openoffice.org/en/forum/ and ask your question there. When you register, supply a fake AOO version for your signature (required to reduce the amount of spam it gets), and edit that to LO once you got in.

Under the ‘Code Snippets’ section in the very well structured forum @floris_v mentioned you will also find code I recently posted which is closely related to your question. It is shaped as a Calc function but handling text in a way also applicable in Writer. The uno-API usage there is derived from Andrew Pitonyak’s famous texts.

Hi

Sub PysReplaceBold

Dim oDocument As Object, oReplace As Object

Dim Attributes(0) As New com.sun.star.beans.PropertyValue

oDocument = ThisComponent
oReplace = oDocument.CreateReplaceDescriptor()

Attributes(0).Name = "CharWeight"
Attributes(0).Value = com.sun.star.awt.FontWeight.BOLD

oReplace.SetSearchAttributes(Attributes())

oReplace.SearchRegularExpression = True
oReplace.SearchString = ".*"
oReplace.ReplaceString = "<b>&</b>"
'replace false by true to also replace bold set with Strong Emphasis (character style)'
oReplace.SearchStyles = false

oDocument.replaceAll (oReplace ())

msgbox "Done"
End Sub

Sub PysReplaceItalic

Dim oDocument As Object, oReplace As Object

Dim Attributes(0) As New com.sun.star.beans.PropertyValue

oDocument = ThisComponent
oReplace = oDocument.CreateReplaceDescriptor()

Attributes(0).Name = "CharPosture"
Attributes(0).Value = com.sun.star.awt.FontSlant.ITALIC

oReplace.SetSearchAttributes(Attributes())

oReplace.SearchRegularExpression = True
oReplace.SearchString = ".*"
oReplace.ReplaceString = "<i>&</i>"
'replace false by true to also replace italic set with Emphasis (character style)'
oReplace.SearchStyles = false

oDocument.replaceAll (oReplace ())

msgbox "Done"
End Sub

Regards