Ask Your Question

Need macro to italicize all text between <i> and </i>

asked 2021-02-22 16:23:29 +0100

robertjacobs0 gravatar image

updated 2021-02-22 17:01:39 +0100

My genealogical program can output rich text , using xxx and yyy for italics. I wish to italicize the marked passages in large documents once I;ve loaded them into LibreOffice Writer.

xxx and yyy use angle brackets i and angle brackets /i which I don't seem to be able to reproduce in this forum.

The documents are in rich text format, not HTML. Here is an example: "the passenger manifest of the S.S. Batavia out of Hamburg."

edit retag flag offensive close merge delete


Really RTF (Rich Text Format) or HTML?

Tell us more about your goal. If your genealogical program outputs HTML, it is more simpler to open this HTML file with Writer (procedure depends on OS which you didn't mention, usually based on a right-click on the file icon). Once in Writer, File>Save as in .odt format does the trick.

Please do not use Add Answer but edit your original question to enhance the details of your question (answers are reserved for solutions to a problem on this Q&A site).

ajlittoz gravatar imageajlittoz ( 2021-02-22 16:38:27 +0100 )edit

Are you aware that the W3C Web Accessibility Initiative (WAI) standards recommend using em and /em instead of i and /i. (Sorry that I can't put my finger on the exact document at the moment.) Many web accessibility testing tools will flag your use of i and /i as a violation. (Angle brackets omitted because it confuses the formatting here.)

ve3oat gravatar imageve3oat ( 2021-02-22 17:06:27 +0100 )edit

I'm not interested in web accessibility. I just want to print out the documents on paper with the marked sections properly italicized.

robertjacobs0 gravatar imagerobertjacobs0 ( 2021-02-22 17:27:30 +0100 )edit

... meaning emphasized. I think current aemphsizing mode can be italic, but isn't explicitly specified this way.

Lupp gravatar imageLupp ( 2021-02-22 17:30:12 +0100 )edit

If the document is truly RTF, open it directly in Writer (eventually give it extension .rtf but this is not strictly necessary). Writer has an input/output filter to import/export RTF files. But be prepared for some errors if your genealogical program uses a different version of RTF specification.

ajlittoz gravatar imageajlittoz ( 2021-02-22 18:40:09 +0100 )edit

Thank you. I experimented by opening the file as RTF and writing it out as ODT. It didn't work, but I may not have attended to the filters you mention. I think the macro supplied by Lupp will do the job.

Thanks to all.

robertjacobs0 gravatar imagerobertjacobs0 ( 2021-02-22 18:49:25 +0100 )edit

"I'm not interested in web accessibility."

Sorry, I thought your intention was to post your genealogy data on the web. My mistake.

ve3oat gravatar imageve3oat ( 2021-02-22 19:11:43 +0100 )edit

I do, but via a different route. I use John Cardinal's GedSite program; see example at (he said proudly) :-)

robertjacobs0 gravatar imagerobertjacobs0 ( 2021-02-22 19:17:40 +0100 )edit

1 Answer

Sort by » oldest newest most voted

answered 2021-02-22 17:31:33 +0100

Lupp gravatar image

Raw code. No checks. no restriction to selected ranges...

Sub applyStyleToAllTextPortionsTaggedHtml4( _
      Optional pCharStyle As String, Optional pTagLetters As String)
REM pCharStyle is expected to be the name of a CharacterStylle having
REM the attribute set. If the style doesnt exist an error is expected,
REM If the name is empty or not passed at all, the default attribute 
REM italic (CharPosture = 1) is set by direct formatting.
If IsMissing(pTagLetters) Then pTagLetters = "i"
searchString = "<" & pTagLetters & ">(.*?)</" & pTagLetters & ">"
If IsMissing(pCharStyle) Then pCharStyle = ""
doc = ThisComponent
searchDescriptor = doc.createSearchDescriptor()
With searchDescriptor
  .SearchRegularExpression = True
  .SearchString = searchString '"<i>(.*?)</i>"
  .ReplaceString = "$1"
  foundRanges = doc.findAll(searchDescriptor)
  u = foundRanges.Count - 1
  For j = 0 To u
    j_rg = foundRanges(j)
    If pCharStyle="" Then
      j_rg.CharPosture = 1
      j_rg.CharStyleName = pCharStyle
    End If
  Next j
End With
End Sub
edit flag offensive delete link more


I think it was explained before on other thread, To do it without macros, can be done in two steps. First using Search&Replace with your regular expression to find all the text to change, at this point all the wanted text it's selected, so a style or a direct format can be applied to all at once.

m.a.riosv gravatar imagem.a.riosv ( 2021-02-22 20:18:25 +0100 )edit

Suppose you mean the linked thread concerning the (roughly) reverse task? I also considered to refer to this interactive way and mention the needed changes. However, I'm not so strict in dissuading from relying on user code if a specifice action may repeatedly occur. I personally tend to not feel really well with repeating manual sequences of actions.

Lupp gravatar imageLupp ( 2021-02-22 22:16:09 +0100 )edit

I only wanted to show another way, not really other, but with or without macro.

m.a.riosv gravatar imagem.a.riosv ( 2021-02-22 22:35:29 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2021-02-22 16:23:29 +0100

Seen: 33 times

Last updated: Feb 22