[Solved] Macro for sequential numbering

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

[Solved] Macro for sequential numbering

Post by Windwood »

Working on a Writer template (.ott) that will contain a sequential number for every time I save using the template. I read and followed the instructions in viewtopic.php?f=13&t=12952 and used the macro code provided there. I'm getting the following exceptions and don't understand how to fix this. Any help appreciated.


Image
Last edited by MrProgrammer on Fri May 27, 2022 8:58 pm, edited 2 times in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
AOo 4.1.13
WIN 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

Can't help with that code - just to say another more common approach is log to a plain text file. The number of the line will be the nth save.

Code: Select all

sub logsaves
dim F,st as string,oPathSettings,pth
F= freefile
st = thiscomponent.url & "," & cstr(now) 
pth = "C:\tmp\OOSaveLog.dat" 'change to your path
  Open pth For Append As #F
    Print #F, st
    Close #F
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

Re: Macro for sequential numbering

Post by Windwood »

Your assumption, and the person's who re posted my queries to this forum is that I have the first clue what you are talking about. I posted my question to the writer forum, where it would have probably been answered with some cogent material, rather than this board, where no one is answering my question. The question has nothing to do with a database or sql. It's about a simple macro, of which I have several implanted in the document that I am working with, all of which came with no hard work or mysterious replies. I do not require a database for this process.
AOo 4.1.13
WIN 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

You posted a database picture and a link to a thread where the solution given was a database... if your question has nothing to do with a database after all... I suggest you try expressing it again making it clear what you're asking...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

Re: Macro for sequential numbering

Post by Windwood »

I believe I did that originally. That which I posted was the result of attempting what I was told to do in order to achieve my end. No need for you to worry yourself about my problem if you don't have a grasp of what I was asking in the first place.
AOo 4.1.13
WIN 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

It appears that nobody has a grasp of what you're asking - as you got no other replies. That's my advice - try again... up to you though...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

Re: Macro for sequential numbering

Post by Windwood »

The original question was:

How to automate a document-number in Writer?

Since I already have a document "date" macro that shows the date after saving, it seems to me that it should be fairly similar to create a macro that sequences a number for every save. I'm not sure exactly how to phrase it, but I just want a sequenced number that will progress to the next digit every time I save a document from my template. I do not require a database or any other form to reference the numbers. I just want the numbers to start at 001 and progress every time I do a save .odt. I'm thinking that someone has already written this and it's a nobrainer, but since my thread got moved over here everyone seems to think I need a database or something.
AOo 4.1.13
WIN 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

How is what you want different from the File menu/properties/General/Revision number?
Revision number:
Displays the number of times that the file has been saved.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

Since I already have a document "date" macro that shows the date after saving,
If you post that and explain how the macro is called maybe someone can help adapt it.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

Re: Macro for sequential numbering

Post by Windwood »

My belief is that the number reflected there only corresponds to the number of times that the template has been edited or revised and not the number of times that a document has been saved by use of that template. If that makes sense. What I need is like a sequential invoice number, so that whenever I type out an invoice, it automatically generates the next number and appends it to that document in some manner. Ideally, it would be appended in text at some designated spot on the document, just as the date macro does.
AOo 4.1.13
WIN 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

What you seem to want is when a document is created from a template the number... 6 if its the 6th document created from that template... is put in the document somewhere? Is that it?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

Re: Macro for sequential numbering

Post by Windwood »

Correctamundo.
AOo 4.1.13
WIN 10
User avatar
Windwood
Posts: 62
Joined: Thu Oct 20, 2011 4:32 am
Location: Cincinnati, Ohio

Re: Macro for sequential numbering

Post by Windwood »

I do not know the answer to this question. I had always thought that there was probably a boiler plate answer, but not one person had suggested anything other that what I have posted and I found nothing when searching, probably because I did not know the right questions to ask. But I appreciate your answer and sometime tomorrow, I will investigate, implement and see if that does the job.
AOo 4.1.13
WIN 10
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro for sequential numbering

Post by RoryOF »

A normal OpenOffice file has within it a counter that marks the number of Saves of that file (note: not the template from which it was created); this counter can be seen in /File /Properties : General tab, Revision number. It might be possible to write a macro that stored a sequential number in an entry in /File /Properties : Custom Properties; such macro would need to be triggered by a specific operation, perhaps a dedicated button. However, if documents such as Invoices were stored under Customer names, the numbering could be specific to that Customer, so there might be duplication of Invoice numbers. Best that the numbering, and perhaps also the Customer details, be retrieved as reports from a database.

If a computerised accounting system is already in place, it may offer such a facility - from memory (circa 2007) QuickBooks certainly did.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro for sequential numbering

Post by Villeroy »

Generating sequences of numbers is an essential feature of your relational database.
See https://ask.libreoffice.org/t/libre-dat ... rt/33779/2 where the attached database shows how to generate invoices as database reports completely free of macro code.
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
Mountaineer
Posts: 316
Joined: Sun Sep 06, 2020 8:27 am

Re: Macro for sequential numbering

Post by Mountaineer »

Windwood wrote:.... I had always thought that there was probably a boiler plate answer, but not one person had suggested anything other that what I have posted and I found nothing ...
Maybe you can read the linked wiki with something like google-translate:
http://www.ooowiki.de/FortlaufendeRechnungsnummer.html

The main piece to answer is, "Where to store the last/next number for an invoice, so it is available for the template".
There is usually no general solution, as everybodys needs are a bit different. While you think as a user of writer there must be a button for this programmers often come to start from a form in a database to generate/edit basic data for the invoice and then generate your writer file as a result. Then the writer-doc is only printed/sent, but receives no buttons at all.

J
OpenOffice 3.1 on Windows Vista
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro for sequential numbering

Post by JeJe »

The attached template has a field named docnumber
Every time a document is created from that template - the field in the created document is set to an incrementing counter that is stored in a file in the user's userconfig folder.

The macro in the template (and copied into the created document) which does this is called by the New Document event.

Code: Select all


sub setSaveNumber
	dim F,n as long,pth,en,ff

	pth = CreateUnoService("com.sun.star.util.PathSettings").UserConfig & "/SaveCounter.dat"

	F= freefile
	alreadyexists =dir(pth)
	Open pth For binary As #F
	if alreadyexists<>"" then Get #F,1,n
	n= n+1
	Put #f,1,n
	Close #F

	en =thiscomponent.textfields.createenumeration
	do while en.hasmoreelements
		ff = en.nextelement
		if ff.gettextfieldmaster.name = "docnumber" then
			ff.gettextfieldmaster.content = n
			exit do
		end if
	loop
End Sub
Attachments
setsavenumbet.ott
(10.35 KiB) Downloaded 110 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply