Hi, i'm literally going crazy to create a simple writer macro. The question is the following.
How can i do to move, through code, to the next record?
I searched a lot on the net but I couldn't find anything, and I'm not sure if it can be done.
Attachments
0_IMG_20230614_210419.png (66.5 KiB) Viewed 5718 times
Last edited by Hagar Delest on Mon Jun 19, 2023 8:03 am, edited 1 time in total.
Reason:tagged solved.
Gix800 wrote: ↑Tue Jun 13, 2023 11:33 am
Hi, i'm literally going crazy to create a simple writer macro. The question is the following.
How can i do to move, through code, to the next record?
For a serial letter, as indicated in your screenshot, you don't have to write any macro code. This works out of the box, pulling the next record at the end of the document.
Simply drag column headers from the right pane of the data source window into the document. This generates the necessary mail merge fields.
When printing, confirm that you are going to print a serial letter.
Then specify either the printer or the file to be used for output.
Edit: Mail merge sample based on the builtin "Bibliography" database:
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Gix800 wrote: ↑Tue Jun 13, 2023 11:33 am
...
I searched a lot on the net but I couldn't find anything, and I'm not sure if it can be done.
...
You didn't look at the right place. Writer has no records. Base has. So start with the guide on Base, if you wish to program a macro.
.
Tell what you really wish to do. For serial letters it is not necessary to program. Just select print, after you put the placeholders in. You will be asked for the necessary records.
LibreOffice 7.6 on Windows 10pro and other Versions parallel
Gix800 wrote: ↑Tue Jun 13, 2023 11:33 am
Hi, i'm literally going crazy to create a simple writer macro. The question is the following.
How can i do to move, through code, to the next record?
For a serial letter, as indicated in your screenshot, you don't have to write any macro code. This works out of the box, pulling the next record at the end of the document.
Simply drag column headers from the right pane of the data source window into the document. This generates the necessary mail merge fields.
When printing, confirm that you are going to print a serial letter.
Then specify either the printer or the file to be used for output.
Edit: Mail merge sample based on the builtin "Bibliography" database:
So you are telling me that it is not possible move to the next record via macro code?
For my purpose I have to achieve this result and not interested mailmerge function.
I'm not familiar with serial letters. If there's no 'proper' way...
If that toolbar is available via the layout manager you should be able to simulate a press on that button using the accessible context - something like this. (element(0) is assumed to be a toolbar, the first item on it accessiblechild(0) a button)
Gix800 wrote: ↑Sun Jun 18, 2023 9:04 am
So you are telling me that it is not possible move to the next record via macro code?
Did you read Mr.dandy proposing?
Gix800 wrote: ↑Sun Jun 18, 2023 9:04 amFor my purpose I have to achieve this result and not interested mailmerge function.
Upload a sample document, you avoid us crystal ball usage.
Attached a simple document linked to a database.
The purpose of the button is to advance a single record forward, but only via code.
I'm embedding all this in a more complex macro that will have to generate as many files as records there are in the database; the names of the files will be taken from column 1, and will be saved inside the folders created with the name extrapolated from column 2.
All this to overcome the limit of the mailmerge function which does not allow to work with the extrapolation of data from two columns .
JeJe wrote: ↑Sun Jun 18, 2023 10:10 am
I'm not familiar with serial letters. If there's no 'proper' way...
If that toolbar is available via the layout manager you should be able to simulate a press on that button using the accessible context - something like this. (element(0) is assumed to be a toolbar, the first item on it accessiblechild(0) a button)
Your signature needs updating if you're using LibreOffice.
The code for finding and pressing the mail merge "Next Mail Merge Entry" button using the accessible context (assuming the toolbar is showing) is this.
sub nextrecord
dim i, j, els
'code to move to next record
els = thiscomponent.currentcontroller.frame.layoutmanager.elements
for i = 0 to ubound(els)
if els(i).resourceurl = "private:resource/toolbar/mailmerge" then
with els(i).realinterface.getaccessiblecontext
for j = 0 to .getaccessiblechildcount -1
if .getaccessiblechild(j).accessiblename = "Next Mail Merge Entry" then
.getaccessiblechild(j).doAccessibleAction(0)
end if
next
end with
end if
next
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
You can write your own SQL query to retrieve the database information - then go through each item of each result to fill in a document you created - without using the mailmerge feature at all.
In fact I tried this code in libreoffice package and got no effect. I'd have to make some attempts to be able to adapt it.
On which parameter should I act to adapt it?
JeJe wrote: ↑Sun Jun 18, 2023 12:16 pm
Your signature needs updating if you're using LibreOffice.
The code for finding and pressing the mail merge "Next Mail Merge Entry" button using the accessible context (assuming the toolbar is showing) is this.
sub nextrecord
dim i, j, els
'code to move to next record
els = thiscomponent.currentcontroller.frame.layoutmanager.elements
for i = 0 to ubound(els)
if els(i).resourceurl = "private:resource/toolbar/mailmerge" then
with els(i).realinterface.getaccessiblecontext
for j = 0 to .getaccessiblechildcount -1
if .getaccessiblechild(j).accessiblename = "Next Mail Merge Entry" then
.getaccessiblechild(j).doAccessibleAction(0)
end if
next
end with
end if
next
end sub
Yesssssssssssssssssssssssssssss!!!
I changed "Next Mail Merge Entry" in my language translation and now it works perfectly!