VBA code to copy contents in a document to spreadsheet

I have a libreoffice writer document. It has several pages, with texts in each page. Now I would like to have the contents in this writer document transferred to Sheet 1, Cell A1 in a Libreoffice calc spreadsheet using a macro.

Is there a code to do that (like Excel VBA)?

In short, I would like to extract the contents of a document into a spreadsheet cell.

The update didn’t provide any additional useful information, imo.
There is still no explanation how “extract” should be understood.
Text documents and spreadsheet documents are completely incompatible by their structures.
If you want to transfer the content of a Writer doc to a Calc spreadsheet, you need to explain a lot about what should go to what cell, how to treat texttables, textframes, headers, footers, everything.
Wihout clear explanations this makes no sense. I’m out now.

LibrOffice doesn’t speak VBA.
It has, however, a poweful API applicable via any programming environment having a sufficient bridge to that API.
The included Basic is poor as a language, but has a wide and short bridge. Also well supported are Python and additional languages. I don’t know anything about support for VB/VBA on this level. There only is a very limited VBA support within LibreOffice Basic which needs to be enabled explictly. I wouldn’t trust in it except for a few very well tested cases.

For me the term “extract” is very unclear. The question seems to be worded without worrying about the chance that a reader (contributor) would understand it, and even less about the time he would need. That’s bad!

IMO, it makes only sense if the contents to “extract” first are clearly defined, and the most practicable way to do it would be a FindAll most likely based on a F&R-likle search with a RegularExpression. You surely wouldn’ like to simply paste the complete content of the source document into a single cell of the spreadsheet, would you?

How to get what you worded “a spreadsheet” and therein the range to paste the “extracted” contents in also is substantial information that isn’t addressed.

We may assume the first sheet of a new spreadsheet document, and therein a range startung at cell A1.

Well, since I sometimes covered similar tasks, I made a demo based on my guesses concerning the vague question. Please take the needed time on the next occasion, to ask a clear question allowing for a clear answer.
See attachment.

@Lupp Good example. Perhaps it is worth adding a check for “u” - if it is less than zero (nothing was found) there is no sense to continue.

@JohnSUN: Thanks! Of course, you are right!
And: There isn’t only no sense in continuing in the case u<0, but it would result in an uncatched error.

Should actually somebody consider to use the code, he (f/m) should insert:

If u<0 Then
 MsgBox("No occurrences found!")
 Exit Sub
End If  

starting with line 12.


if u < 0 then
	sheet.getCellByPosition(0, 0).String = "No occurrences found!"
	exit sub

after line 13 :wink:

Hmmm… :-o)