Replace every number in a document with its *2 product

I am very new to Basic and would like some help with a problem.
I’m trying to find every number in a document multiply it by 2 and replace it with the result.

I would appreciate any help I get

I think: cou can do this witout any macro. The Paste Special feature has an arithmetic modifier function. Study it, and try it.

I am very new to Basic

Please download and study Andrew Pitonyak’s free macro books.
And download one of the excellent Object inspection tools: XrayTool or MRI. Then you will able to list and examine the properties and methods the programming objects like the document, the sheet, or the cell…

Note that the question is tagged writer.

Sorry, I have not noticed that…

In this case:

  • Maybe the writer is a wrong tool for this task.
  • The recommended books may will help for start.

The “numbers” are textual characters in a writer document (except in the Tables, where the LO can make some calculation with them), therefore it must decide: the goal is
“1818” when the “original number” is “99” or is the excepted result “198”?

https://help.libreoffice.org/latest/en-US/text/shared/01/05020301.html?&DbPAR=CALC&System=WIN
search for ‘NatNum modifiers’

Welcome!

Every? Or don’t touch page numbers and numbered lists? And also the numbers of inserted images, frames and other numbered objects?

You will help us a lot if you give an example of what you want to receive. For example, “After executing the macro, the text The amount of the contract for the supply of 10 (ten) computers is 15327 coins should look like The amount of the contract for the supply of 20 (ten) computers is 30654 coins, and I will replace the word (ten) with (twenty) manually”

2 Likes

@JohnSUN I think it is tagged »writer« by mistake?!

Usage the Labels and an user defined macro and the Numbertext extension (developed basically for the Calc) maybe can help in this case.

The “Numbertext” extension is obsoleted long time ago (by the Numbertext author, László Németh), when version 6.1 introduced “spell out” number format, usable in any place where you can define number format codes - be in Calc’s TEXT function and cell formats, or Writer fields, or Basic Format function, etc.

1 Like

The example you gave is pretty much what I’m trying to achieve, so in a given text find the numbers and multiply them by 2. It’s a practice thing and I have no clue how to solve this in order to move forward.

But thanks to all of you who have replied so far, I appreciate it.

Not answering your direct question (which, likely, is based on a need to handle some existing documents):

the proper way to create documents with numbers and calculations is using variable fields and simple calculations in Writer, so that later you may change variable values in a central location, and have all the calculations update automatically.

1 Like

As the simplest way to complete the task:

Sub EachNumberTwice
Dim oSearchDescriptor As Variant
Dim oFound As Variant
Dim i As Long
Dim oNextNumberString As Variant
Dim sString As String
	oSearchDescriptor = ThisComponent.createSearchDescriptor()
	oSearchDescriptor.setSearchString("\d{1,}") ' or oSearchDescriptor.setSearchString("\d{1,}[\.|,]?\d*") ? '
	oSearchDescriptor.SearchRegularExpression = True
	oFound = ThisComponent.findAll(oSearchDescriptor)
	If Not IsNull(oFound) Then 
		For i = 0 To  oFound.getCount() - 1 
			oNextNumberString = oFound.getByIndex(i)
			sString = oNextNumberString.getString()
			oNextNumberString.setString(Val(sString) * 2)
		Next i
	EndIf 
End Sub

Can you read and understand it? Or do you need more clarification? Feel free to ask clarifying questions - we will be happy to help you deal with this problem.

1 Like

Just at first sight: is there one } without matching { , or is this something I should learn. (RegExp seems to be a topic, where I’m always learning…)

No, no, it’s just a typo - instead of the second closing curly brace, it should have been “pipe” |. Meant “either a dot or a comma”. Of course, in the case of such a search, the multiplication procedure will be more complicated …

No, you don’t need the pipe in the “set of characters” square brackets regex operator - and it will actually mean a different thing there. You need to simply list al the characters, and the operator will match any of them, so already means “any one of the set”: [.,]. In the brackets, most regex special characters loose their special meaning (some have the special meaning still, sometimes based on their position in the list), and treated literally - so the pipe there would in fact mean “either dot, or comma, or a pipe”.

1 Like

Thank you so much, I am slowly beginning to understand how it works and you’ve saved me a lot of headaches with this code snippet.

Dear Mike, how you can format the user defined properties what are used as Fields in the body of a Writer document?

:slight_smile: Did you notice the “where you can define number format codes” words? Indeed, if a UI doesn’t provide a way to define number format, you will be unable to define it. You are welcome to file an enhancement request to allow defining format for the field.

My workaround: I am using two Fields and an input box for entering a new numeric value. My macro will format the number, and calls the NUMBERTEXT() or the MONEYTEXT() function of the Calc. (The Numbertext extension is installed) Then the macro put the numeric and the textual results into the two Fields (into the two user defined document properties, what appeared as Fields in the Writer document)
(it works in LO 6.1.6)

… should use built-in Format function with the [NatNum12] format specifiers, and not need any extensions - did you notice the “or Basic Format function” words? :wink: It works with 6.1.6, too.