How do I capitalize (via macro) the first letter of a name on Calc?

I have this code that works, but I need to use “Option VBASupport 1”
Does anybody know a way to do the same macro code on Basic pure language?

Option VBASupport 1
Option Explicit
Function FirstLetterUpperCase (sWord as String) as String
'Dim sWord as String
'sWord = “abc EFG hij”
FirstLetterUpperCase = StrConv(sWord, vbProperCase) ’ = “Abc Efg Hij” vbProperCase)")
End Function

1 Like

If you hadn’t explicitly asked for “basic pure”, I would answer with:

mystring = "abcdefg"
print( mystring.capitalize())

# > "Abcdefg"
## I need to make a correction here, to capatilize every word in a given String:
otherstring = "RsfZ ßZK nnÄÜÖ Öfh"
print( otherstring.title() )
## →'Rsfz Sszk Nnäüö Öfh'
### of course thats pythoncode

but so I do not answer you.

If you would ask for Calc, i would answer:

=PROPER("abc EFG hij”)

Sorry, sorry my English…your macro code doesn´t work…I meant Libreoffice Macro Code…I get a error from your code.

I already knew this formula code “=Proper”. But I need a macro code, like the Excel Macro Code “VbProperCase”…I saw many examples that use Upper(LEFT(string, 1)),but this code becomes very complex with a many words people name (for example). I want a simple one line code…If it´s possible, of course.

The code works for Calc macros written in Python, but not in Basic.

I don´t know Python (for me, it’s a gigant snake)

pure Basic with some help from Calc

Sub Main
	fu = createUnoService("com.sun.star.sheet.FunctionAccess")
	prop = fu.callFunction("proper", array("abc DEf ghj"))
	print prop
End Sub
1 Like

I get the error 91…
BASIC runtime error. ‘91’
Object variable not set

show your complete Code… copy&paste between three backtics ``` above and below the codeblock

It´s exactly yours…I just copied e pasted your code and I tested it…I use Calc.
First I got the error 12…then I declared fu as Object and then I get the error 91 in line with “prop” (that I declared it as variant

It should work as posted. The error must be coming from somewhere else in your code.

If Option Explicit is set then declare the variables first.

Dim fu As Object
Dim prop As String
Sub UppercaseWords
Dim fu as object
Dim prop as variant
Dim sMyString as String

sMyString = "abc DEf ghj"
fu = createInstance("com.sun.star.sheet.FunctionAccess")
prop = fu.callFunction("PROPER", array(sMyString))
print prop
End sub

This is my code…I changed little things.

1 Like

Don’t use smart quotes - they need to be normal quotes for programming. Also it’s CreateUnoService, not CreateInstance.

Sub UppercaseWords
Dim fu as object
Dim prop as variant
Dim sMyString as String

sMyString = "abc DEf ghj"
fu = createUnoService("com.sun.star.sheet.FunctionAccess")
prop = fu.callFunction("proper", array(sMyString))
print prop
End sub

You are amazing…It´s worked…More one that I learned…THANKS A MILLION.

Why the hell did you createInstance instead createUnoService

I got the same error…I searched on internet and I changed it…but now, maybe, with the declaration of variable, it worked…My mistake…Sorry…Thanks for your patience.

:hot_face:

2 Likes

Sorry, sorry…I changed after the error, but in the end, you are totally right. Thanks.

Glad to hear it has been resolved. One last thing you need to do, mark the answer by karolus as the solution.

I marked the answer…Thanks…You all are The Macro Master of The Universe.