Of course, the file(s) must be opened.
You need to identify the file(s) by correct full pathname(s).
This may be done with the help of string formulas. Finally you have a full pathname in one cell and can use it as the parameter value for the following user function which you call from a (best adjacent) different cell.
Function wordCountExternalFile(pFileIdentifyer As String)
REM Full absolute pathname required here.
wordCountExternalFile = 0
On Local Error Goto finally
theUrl = ConvertToURL(pFileIdentifyer)
If NOt FileExists(theUrl) Then Exit Function
Dim args(0) As New com.sun.star.beans.PropertyValue
args(0).Name = "Hidden"
args(0).Value = True
docExt = StarDesktop.loadComponentFromURL(theUrl, "_blank", 0, args)
wordCountExternalFile = docExt.WordCount
finally:
On Local Error Goto finished
docExt.Close(True)
finished:
End Function
This can only work for files for which a WordCount property is created. Plain text files with the extension .txt will do …