How to alter macro to allow special characters in filename

asked 2019-07-20 20:58:56 +0100

CarlB gravatar image

updated 2020-08-08 23:35:19 +0100

Alex Kemp gravatar image

Am using LibreOffice 6.3.01 x64 on Windows 10 64bit

The macro below doesn't seem to allow for special characters in my filename (e.g. comma or single quote).

How do I need to change the macro so that it allows all valid filename characters please?

REM  *****  BASIC  *****
Option explicit

Function MYFILEEXISTS(sFullPath as string) as boolean

dim sFileUrl as string

   sFileURL = ConvertToUrl(sFullPath)   

   If FileExists(sFileUrl) then 
      MYFILEEXISTS = False
   end if
End function
edit retag flag offensive close merge delete


In order to hardcode, you need to encode them as in URLs (such as %2C for a comma, see this). I don’t know how to do it in StarBasic, though, meaning, if there is a dedicated function.

gabix gravatar imagegabix ( 2019-07-20 22:04:42 +0100 )edit


I have just tested this procedure on windows and it works as expected with file names like a,b.html or a'b.html

Besides, I'm probably from an old world but I'm not completely sure it's a good practice to name files like that :)


pierre-yves samyn gravatar imagepierre-yves samyn ( 2019-07-21 07:48:17 +0100 )edit

Hmm - I ask myself whether this is a question to be addressed to users of LibreOffice. The macro above relies on the Basic function ConvertToUrl and if this function cannot deal with filenames specific to an operating system, then this may be considered a bug of LibreOffice Basic (or at least an enhancement request may be required)

Opaque gravatar imageOpaque ( 2019-07-21 13:11:46 +0100 )edit

What does the "sFileURL = ConvertToUrl(sFullPath)" actually do? Is it needed at all given all my filenames are already full pathnames?

CarlB gravatar imageCarlB ( 2019-07-21 16:01:14 +0100 )edit

As far as i understand function FileExists allows two modes to pass a filename

  • system notification -- and --
  • URL notification

(see here). And function ConvertToUrl - nomen est omen - converts the text string to URL type. According to FileExists your macro should also work, if you

-- dim sFileUrl as string
-- sFileURL = ConvertToUrl(sFullPath)  
-- If FileExists(sFileUrl) then 
++ If FileExists(sFullPath) then

(Did not yet test)

Opaque gravatar imageOpaque ( 2019-07-21 17:16:18 +0100 )edit

@Opaque - I have changed macro and tested as suggested but it makes no difference. Perhaps the problem is with my formulae rather than the macro itself?

=IF(MYFILEEXISTS(C7018),IF(OR(K7018="Y",N7018="",N7018="DVD",N7018="CD"),"not online",HYPERLINK(C7018,"Click")),"not exist")

This returns "not exist" for any filename that contains a single quote. Can you suggest how I should alter please?

CarlB gravatar imageCarlB ( 2019-07-22 14:38:14 +0100 )edit

It got nothing to do with your formula but the interpretation of apostroph and single quote and the replacements performed by Calc, when you input a ' into Calc text and thus the filename in your sheet changes. See Tools -> AutoCorrect Options -> Tab: Localized Options. The option Replace in category Single Quotes is ticked by default, and this causes a change in the string of the filename. Try to untick and check again - during my test this made it working.

Note; Do not test with filename strings, where the change already has been occurred. Insert a new filename.

Opaque gravatar imageOpaque ( 2019-07-22 15:48:18 +0100 )edit