2012/7/17 Johnny Rosenberg <[email protected]>:
> 2012/7/17 Srinivasulu Bhattaram <[email protected]>:
>> I am new to Open Office.
>> InWORD 2003 (and also in WORD 2007),
>> I have craeted macros to remove double spaces with single spaces and used
>> them extensively..
>> How to have an equivalent of it in Open Office writer?
>>
>> I do not want to go through   Find    Replace.... kind of thing.
>> seena
>
> What about letting a macro do the search and replace for you?
>
> I just wrote this one, with inspiration from ”Useful Macro Information
> For OpenOffice.org By Andrew Pitonyak”, which is a PDF that you can
> download somewhere:
>
> REM  *****  BASIC  *****
>
> Sub RemoveRedundantSpaces
>         Dim oReplace as object
>         oReplace = ThisComponent.createReplaceDescriptor()
>
>         With oReplace
>                 .SearchString = "  "
>                 .ReplaceString = " "
>         End With
>
>         While ThisComponent.ReplaceAll(oReplace)>0
>         Wend
> End Sub
>
>
> This macro also takes care of tripple spaces and… well, it just
> removes all spaces until there are only single spaces left. And it is
> fast. Really fast.
> I tested it myself, and it worked in all my test cases. Well, I didn't
> test it THAT thoroughly, but still…
>
> Much better than using the crappy macro recorder anyway. And shorter…
>
> If you write the documents yourself, there is a simple way to prevent
> double spaces in the first place, something like:
> Tools → Options for auto correction… → Click the Options tab → ☒
> Ignore double spaces
>
> You can still make double spaces if you really want to, but it's less
> likely to happen accidently.
>
>
> Kind regards
>
> Johnny Rosenberg
> ジョニー・ローゼンバーグ

Here is yet another macro, but this one replaces more things than just
double spaces, and you can easily modify the macro for your own
purposes, take a look at the comments in the code (the lines starting
with a single quote):

REM  *****  BASIC  *****

Option Explicit

Sub RemoveRedundantSpaces
        Dim oReplace As Object
        oReplace = ThisComponent.createReplaceDescriptor()

        Dim iTotalCount As Integer

'       Replaces two spaces (or more) with one single space. ”False”
'       means that regular expressions is not used.
        iTotalCount = iReplace(oReplace, "  ", " ", False)

'       A few other replacements follow below. You can easily add more
'       or remove those lines you don't want.
        iTotalCount = iTotalCount + iReplace(oReplace, " .", ".", False)
        iTotalCount = iTotalCount + iReplace(oReplace, " ,", ",", False)
        iTotalCount = iTotalCount + iReplace(oReplace, " :", ":", False)
        iTotalCount = iTotalCount + iReplace(oReplace, " ;", ";", False)
        iTotalCount = iTotalCount + iReplace(oReplace, " !", "!", False)
        iTotalCount = iTotalCount + iReplace(oReplace, " ?", "?", False)

'       Here are a few using regular expressions; the last parameter is set
'       to ”True”. The first one removes spaces at the end of paragraphs,
'       the next one removes spaces at the beginning of paragraphs.
'       Feel free to add or remove whatever you like.
        iTotalCount = iTotalCount + iReplace(oReplace, " $", "", True)
        iTotalCount = iTotalCount + iReplace(oReplace, "^ ", "", True)
        
        Select Case iTotalCount
        Case 0
                Print "No redundant spaces was found."
        Case 1
                Print "1 space was removed."
        Case Else
                Print iTotalCount & " spaces were removed."
        End Select
End Sub


Function iReplace(oDescriptor As Object, sSearch As String, sReplace
As String, RegExp As Boolean) As Integer
        With oDescriptor
                .SearchRegularExpression=RegExp
                .SearchString = sSearch
                .ReplaceString = sReplace
        End With

        Dim iCount As Integer, iSumCount As Integer
        Do
                iCount=ThisComponent.ReplaceAll(oDescriptor)
                iSumCount=iSumCount+iCount
        Loop While iCount > 0
        iReplace=iSumCount
End Function

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to