Ask Your Question

Macro - How to select text in a table cell and use that to replace other text in a document

asked 2018-07-22 10:52:29 +0200

SkowGoing gravatar image

Using a macro, how do I select text in a table (ie A1) and use that string to replace another string in the same document. I find the Basic programming documentation quite hard to search and interpret


edit retag flag offensive close merge delete


For Calc, or Writer?... A file with an example of what you want always helps anyone who wants to help you.

mauricio gravatar imagemauricio ( 2018-07-22 19:18:27 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2018-07-23 11:56:13 +0200

SkowGoing gravatar image

updated 2018-07-28 12:19:24 +0200

It is for writer. I have been reading quite a bit on the OO site and when using Cell.getValue() it returns the floating point value but not the text

I'll post the macro when I get home

Here is the macro. If no table is in writer it will create one and if the table is already there it get deleted. Should work on a blank document. However, I cannot read a cell back Any suggestions

Sub InsertDeleteTable Dim oTable 'Newly created table to insert Dim oTables 'All of the text tables Dim oInsertPoint 'Where the table will be inserted Dim sTableName as String

sTableName = "TableCustomerInfo" oTables = ThisComponent.TextTables

If oTables.hasByName(sTableName) Then oTable = oTables.getByName(sTableName)

REM Although this seems like the correct way to remove text content,
REM what if the table is not inserted into the primary document's
REM text object? oTable.dispose() may be safer to use.


REM Let the document create the text table.

oTable = ThisComponent.createInstance( "" ) oTable.initialize(2, 3) 'Two rows, three columns

REM If there is a bookmark named "InsertTableHere", then insert
REM the table at that point. If this bookmark does not exist, 
REM then simply choose the very end of the document.

If ThisComponent.getBookmarks().hasByName("InsertTableHere") Then oInsertPoint =_ ThisComponent.getBookmarks().getByName("InsertTableHere").getAnchor() Else oInsertPoint = ThisComponent.Text.getEnd() End If

REM Now insert the text table at the end of the document.
REM Note that the text object from the oInsertPoint text
REM range is used rather than the document text object.

oInsertPoint.getText().insertTextContent(oInsertPoint , oTable, False)

REM The setData() object method works ONLY with numerical data.
REM The setDataArray() object method, however, also allows strings.

oTable.setDataArray(Array(Array("A0", "B0", "C0"), Array("A1", "B1", "C1")))

oTable.setName(sTableName) End If '-------------------------------------------- 'replace text Dim newText(1) as String Dim oldText as String

newText =  oTable.getDataArray(A1)

msgBox (newText)

End Sub

edit flag offensive delete link more

answered 2018-07-23 12:08:48 +0200

In addition to XCell, the cell objects in Writer also expose XTextRange interface, with its getString().

You may use tools like MRI/XRay, or you may use the integrated Watch tool, using which, you may expand Types under a watched variable, and see all types and methods and much more for any variable.

edit flag offensive delete link more


Primo, didn't know about XTextRange. Is there a good basic writeup about MRI anywhere ?

SkowGoing gravatar imageSkowGoing ( 2018-07-23 22:12:12 +0200 )edit

Hmm, MRI is refusing to load on Version: (x64) win10. ( { { Message = "<class 'syntaxerror'="">: invalid syntax (, line 21), traceback follows\X000a File \"C:\Program Files\LibreOffice\program\\", line 149, in writeRegistryInfo\X000a mod = self.getModuleFromUrl( locationUrl )\X000a File \"C:\Program Files\LibreOffice\program\\", line 102, in getModuleFromUrl\X000a codeobject = compile( src, encfile

SkowGoing gravatar imageSkowGoing ( 2018-07-23 22:29:22 +0200 )edit

Personally I don't use MRI, so cannot suggest any advise; problems in it should go to its bug tracker.

I prefer the built-in watch to look into objects.

image description

Mike Kaganski gravatar imageMike Kaganski ( 2018-07-24 01:08:26 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-07-22 10:52:29 +0200

Seen: 405 times

Last updated: Jul 28 '18