A string array without specified dimensions

Creating a macro - Writing a Script - Using the API

A string array without specified dimensions

Postby gutee » Tue Jan 22, 2019 5:16 am

I need to define one Array, the element inside will be String, but I do not know how many elements at the beginning.
my code is :
Dim my_str_arr() As String
my_str_arr(1) = "Test_Str"
It will show error : Inadmissible value or data type. Index out of defined range.

It works if I define the Array with dimensions defined.
Dim my_str_arr(2) As String
my_str_arr(1) = "Test_Str"
Posts: 19
Joined: Wed Jan 09, 2019 9:12 am

Re: A string array without specified dimensions

Postby FJCC » Tue Jan 22, 2019 6:20 am

You can use
ReDim Preserve
to change the size of an array while preserving the content. Please see the Help section for information about that command.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Posts: 7084
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: A string array without specified dimensions

Postby robleyd » Tue Jan 22, 2019 6:36 am

See https://wiki.openoffice.org/wiki/Docume ... ide/Arrays for ways of varying the number of elements in an array via REDIM, or see the section on empty arrays. Note that page also says "There are no practical limits on the indexes or on the number of elements in an array, so long as there is enough memory".

I suggest you read all of the page as you are likely to have other array questions which can be answered by reference to that page.
Apache OpenOffice 4.2.0 Build 9820 alpha version - Slackware 14.2 - 64 bit
LibreOffice - Slackware 14.2 - 64 bit
Apache OpenOffice 4.1.4 - Windows 7 Virtual machine
User avatar
Posts: 2568
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: A string array without specified dimensions

Postby JeJe » Tue Jan 22, 2019 11:08 am

One Caution... because of some minor annoying faults with OO it can be better to declare my_str_arr() as Variant instead of string. If you're going to use the split function for example...

Openoffice 4.1.2
Windows 8
Posts: 457
Joined: Wed Mar 09, 2016 2:40 pm

Return to Macros and UNO API

Who is online

Users browsing this forum: arfgh and 6 guests