How to make either a function or a macro that would be able to add +1 to a cell every time it loads a new templates

So i was looking for a function or macro that that would allow me to do this (example below):

I want a cell to add +1 to a cell every time a export has been made to be able to create a invoices for this company that would keep track of each invoice.

It would work something like this

opens template (invoice cell # ) [001] —> exports to pdf —> Next time the template loads up (invoice cell #) [002]

I want to automate this process.

Thank you for your help ! :slight_smile:

And what about when the user open a new template twice, but not export any PDF? It need to increase the number or not?
.
It will not work as a macro FUNCTION. You need a SUBROUTINE for it. A subroutine can run when an Event happened or by User activity.
.
Is the counter spreadsheet same as the invoice template, or is there a standalone “bookkeeping” spreadsheet for the counter and other data to register about the invoices?

And what about when the user open a new template twice, but not export any PDF? It need to increase the number or not?

If that happens its should not increase by one until it is exported by pdf.

You can have two open on accident or not but it would not increase the # until its exported.

The way i have planned it out in my head was i created a macro and bind it to a event. The only problem I run into is that the template doesnt save that value for the next time to keep added onto the (invoice cell value)

[Basic macro]

Sub CellPlus1
	Dim oSheet as Object
    	oSheet = ThisComponent.getSheets().getByIndex(0)
    	
oSheet.getCellRangeByName("F2").setValue(oSheet.getCellRangeByName("F2").getValue() + 1)

End Sub

Is the counter spreadsheet same as the invoice template, or is there a standalone “bookkeeping” spreadsheet for the counter and other data to register about the invoices?

The Counter is keep track of invoices that as been created and to have a unique but sequential number for each invoice.

Where you want to store the increased number? In the Template, or in a standalone spreadsheet file? The dinamic programming variables are located in the memory. They will be destroyed when you switch off the computer. You must archive the increased number somewhere. The macro must get the LAST value to increment it again. It is very difficult to get the number from the last PDF export.

Then you need an “Not exported/Exported” boolead flag (user defined property or dedicated cell) into the actual document. Otherwise you can export twice or more accidentally - with repeatedly incremented number.

Where you want to store the increased number? In the Template, or in a standalone spreadsheet file?

The variable will most likely come from the template.

Basically I want to able to save the value in the template so i am able to access it again when it the user loads the template again to keep the number incrementing

  • Then you must increase the number and must RESAVE the template before you have modified/actualized it. Otherwise you will resave a modified template.
  • Or you can reopen the lastly saved template again (even silently) after the increasing the number and then you can set the new number in the template, then you can resave it by your macros without any other modifications.

Please study Andrew Pitonyak’s free macro books. Based on his macro code snippets, and after a detailed planning phase - what we started in this site together with you - you will able to write such macro.
https://www.pitonyak.org/oo.php
Download, install and use one of the excellent Object Inspection Tools: XrayTool or MRI. Then you will able to list and examine the existing properties and methods of the programming objects like the Document, Sheet, Cell, User Defined Properties, and more others.
https://berma.pagesperso-orange.fr/index2.html

Concerning terminology:

  • Templates are specialized files used to create new files with some predefined attributes and some content. The new file gets a provisional title (“Untitled 1” e.g.) and the template will not be open after this process. It will therefore also not be informed of what happened to the new file, and cannot save any related data.

  • In a non-professional environment, the practice may be to derive a new document (letter, invoice, data table) from an ordinary document of the same type, using it as an example or protoype. This is not recommended: There are too many possible errors that are difficult to fix.

In the given case it’s unclear what actually is meant, and what proceeding is established.

  1. Where do the data to be inserted into a new invoice come from?
  2. Shall new data also be inserted on the fly and saved together with what you called a template? In this case, how is the integrity of the contents of the master copy and the protection against data loss in the event of system malfunctions taken care of?
  3. Why don’t you create and save the number of the new invoice together with the data it’s based on?
  4. How is the export process triggered?
  5. How many invoices do you usually print per day / per week?

The question of how a single cell can be automatically incremented cannot be seriously answered separately from the rest of the workflow.
If tried such an answer could only be like “Study the attached example.”
disask86219AutoIncrementAndStore.ods (10.3 KB)

Yes, the full path and file name (URL) of the template must be coded in the macro.

Invoice_00000.ott (97.7 KB)
Open the template for editing, open the contained macro and adjust the path of invoices with trailing slash or backslash.

Every time you click the toolbar button attached to the template, a new document 10000.odt, 100001.odt, 100002.odt will be saved. The file name without extension is referenced as invoice number.

P.S. Calc formula to get the invoicec number from filename:

=REGEX(CELL("filename");"^.+/(\d{5})\.ods'#.+$";"$1")

the macro remains the same with “.ods” instead of “.odt”

What’s the point in misusing my UserName?

:question:

I actually missed this point:
You used the ‘Sender’ variable based on the user profile for TexFields in the “logo” of your template (and in two different places) choosing ‘Forename’ first and then ‘Surname’. However, I have chosen to enter unusual content as UserData in my currently active profile and got “Lupp” and “” there.

Forget about?

BTW: I next to always use the Format ‘Familienname’ ‘Vorname’, and my signature is based on ‘FamilienName’ ‘EinzelInitiale’ (Jäger W.). You may know my dogm
“If compounds at all then sortable to the expected effect”
and Jäger W. comes before Säger A.