Ask Your Question
0

Macro to bold words

asked 2020-11-21 13:52:46 +0100

Murtaza_R gravatar image

Hi, I am trying to make a macro which takes an input of some words in an input dialog box, finds all those words in the document and bolds them. I recently switched to Linux and this is crucial for some of the work that I do. I had a macro for Microsoft Word that did the same, but can't seem to figure out how to do it in LibreOffice Writer.

My macro for Microsoft Word that I used on Windows;

    Attribute VB_Name = "Bold_All_Keywords"
Sub BoldAllKeywords()
    Dim xFind As String
    Dim xReplace As String
    Dim xFindArr, xReplaceArr
    Dim I As Long
    Application.ScreenUpdating = False
    xFind = InputBox("Enter items to be found here,seperated by comma: ", "Bold all Keywords")
    xReplace = InputBox("Enter new items here, seperated by comma: ", "Bold all Keywords")
    xFindArr = Split(xFind, ",")
    xReplaceArr = Split(xReplace, ",")
    If UBound(xFindArr) <> UBound(xReplaceArr) Then
        MsgBox "Find and replace characters must be equal.", vbInformation
        Exit Sub
    End If
    For I = 0 To UBound(xFindArr)
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Replacement.Font.Bold = True
            .Text = xFindArr(I)
            .Replacement.Text = xReplaceArr(I)
            .Format = True
            .MatchWholeWord = True
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next
    Application.ScreenUpdating = True
End Sub

Any help with a similar macro for LibreOffice would be great! TIA!

edit retag flag offensive close merge delete

Comments

I have not written any macro yet so I can't help you much. However I suggest an improvement.

Write has the notion of character styles, i.e. a named set of formatting attributes to be applied to a range of characters inside a paragraph. Create a character style named Keyword. In your macro, apply this Keyword character style instead of directly bolding the words.

If afterwards you consider your keywords should be red, monospaced font face and italic instead of bold, you only need to update your Keyword character style to change everything in the document without the need to hunt for the keywords or rerun your (modified) macro.

ajlittoz gravatar imageajlittoz ( 2020-11-21 14:45:17 +0100 )edit

There is OpenOffice.org Macros Explained, which as a chapter "14.2.2. Character properties". You're looking for "CharWeight". Or create a style, like ajlittoz suggested, and apply that.

jmux gravatar imagejmux ( 2020-11-21 15:04:24 +0100 )edit

Thank you all. I found a macro in the book jmux recommended, needs some modification, but I think I can change it to what I need.

As far as setting a character style is concerned, I don't think it'll work for what I am trying to achieve as the words to turn to bold are going to be changing frequently. Would be a pain to go through the settings every time and change the words.

Murtaza_R gravatar imageMurtaza_R ( 2020-11-21 15:13:44 +0100 )edit

Why would it be a pain? Once you've styled the words, they remain styled. If you need to change "Keyword-A" for "Schlüsselwort-A", use Find & Replace, it won't change the style, only the characters.

In a frequently changing document, styles are really your friends. Learn how to use them. You'll wonder how you could work without them due to the comfort they bring.

ajlittoz gravatar imageajlittoz ( 2020-11-21 15:21:07 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-11-21 15:51:29 +0100

Lupp gravatar image

As already mentioned usage of styles is recommendable.
However, without knowing how to explain it in short, I can imagine cases justifying a different approach.
Anyway I wrote a macro for the task as described. It is contained in the attached example document and can be tested there. C:\fakepath\boldifyFoundTextPortions.odt

edit flag offensive delete link more

Comments

This works perfectly. Thank you so much!

Murtaza_R gravatar imageMurtaza_R ( 2020-11-22 16:39:07 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-11-21 13:52:46 +0100

Seen: 33 times

Last updated: Nov 21