Macro function regex not working

Libre Writer Macro function string manipulation with regex not working.

LibreOffice version 7.6.1.2(x86_64) on Windows 10

I use the following:

func getFileName
sUrl = ThisComponent.getURL()
sFullPath = ConvertFromURL(sUrl)
sFileName = Regex(sFullPath;"[^\]*$")
msgbox(sFileName & Chr$(13) & filename)
end func

No matter what I do the function keeps returning an error "Sub procedure or function procedure not defined.

If I leave out the quote in the regex definition I get the error “Unexpected symbol ^\”

The regex command [^\]*$ works. I tested it using search & replace in Writer where I inserted a full path to a file. The search highlights the file name correctly. And I tested it at https://regex101.com/. with a full path file statement.

I’m open to an easier way to get the file name into a variable.

John in Oregon USA

Welcome back, John! Haven’t seen you for a long time - hope you’re okay?
To be honest, I was greatly confused by this strange word “func”, the call to the “Regex” function that was not described anywhere, and the attempt to display the “filename” variable, which was not calculated anywhere, in MsgBox. This is actually done like this:

Function getFileName As String 
	GlobalScope.BasicLibraries.LoadLibrary("Tools")
	getFileName = ConvertFromURL(FileNameoutofPath(ThisComponent.getURL(), "/"))
End Function

Sub testFileName
	MsgBox(getFileName())
End Sub
2 Likes

I hope you mean:⠀⠀ [^\\]*$

1 Like

The ConvertFromURL here is not needed (and is even dangerous). FileNameoutofPath calls it internally, when the optional Separator is missing; and calling it again might URL-decode characters again, which might break, when the file name saved locally is really some weird foo%20bar.txt, which in URL was e.g. file:///c:/foo%2520bar.txt.

1 Like

Thanks. I used cwolan’s answer but good to know I was mixing Function and Sub. At least now I don’t hate the writer(s) of Tool LOL.

See how to use Calc Functions in Macros

Otherwise see if String service – ReplaceRegex is helpful.

Thanks for your help. Great that you responded so fast.

I checked those out. I understood most of it a little but not enough to actually put it into a function.

There is a “Tools” functionFileNameoutofPath but it’s not working - throws errors.

I do have a way to split the full path into parts, and I think the last part would have the file name but I don’t know how to put the last array item into a variable. In other words, I don’t know how to address the last array item.

sUrl = ThisComponent.getURL()
sFullPath = ConvertFromURL(sUrl)
sParts = Split(sFullPath,"\")
ReDim Preserve sParts(0 to UBound(sParts) - 1)
 sDirectory = Join(sParts, "/")

I can get the directory out of that (sDirectory) but not the file name.

sParts is an array apparently, but as I say I can’t figure out how to get to the last array item to get the name.Preformatted text

Sorry about the bolding, I was trying to get some separation for the code but I don’t know how to use this forum and my dashes resulted in that bolding.

'…
url = ThisComponent.getURL()
parts = split( Url,"/")
name = parts(ubound(parts))
msgbox name

edit: in case there are ugly spaces, …whatever in name you need:

name = convertfromurl(parts(ubound(parts)))
1 Like

name = parts(ubound(parts)
give error: Inadmissible value or data type. Index out of defined range.

because your current »thisComponent« isnt stored, but some new from scratch with name untitled1 ??

I got it. I hadn’t save the document, stupid me sometimes.

Problem now is the result include %20 for spaces. So I used replace:

name = replace(name,"%20"," ")

And it works!!! Thank you so very much. Now I think tomorrow I’ll try to figure out why it works, but at least my project can continue. Whoopee!

see my edit:

1 Like