Macro Error

Creating a macro - Writing a Script - Using the API

Macro Error

Postby supremegrandruler » Fri Apr 18, 2014 3:05 pm

Not sure why but getting an "argument is not optional" error. Here's the code:

Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

Function CardCost(Optional Atk as integer, Optional Hp as integer, Optional Mvt as integer,  Optional Rng as integer) as integer
rem ----------------------------------------------------------------------
   
   If Mvt<=0 then
      Mvt=1
   endif
   
   CardCost=(((Atk+Hp)*(Mvt*0.25)*(Rng*0.4))*0.9)

end function
OpenOffice 3.4.1 MacOS X 10.9.1
supremegrandruler
 
Posts: 19
Joined: Mon Feb 17, 2014 8:28 am

Re: Macro Error

Postby Villeroy » Fri Apr 18, 2014 6:32 pm

Because this is Basic and Basic requires that you use the IsMissing(Mvt) function to test if var is missing before you compare it to a number.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 27731
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro Error

Postby supremegrandruler » Sat Apr 19, 2014 2:26 am

I changed it to:

Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

Function CardCost(Optional Atk as integer, Optional Hp as integer, Optional Mvt as integer, Optional Rng as integer) as integer
rem ----------------------------------------------------------------------
   If NOT IsMissing(Mvt) Then
      If Mvt<=0 then
         Mvt=1
      endif
   endif
   CardCost=(((Atk+Hp)*(Mvt*0.25)*(Rng*0.4))*0.9)

end function


Now I get "inadmissible value or data type". Never had problems with this macro before...
OpenOffice 3.4.1 MacOS X 10.9.1
supremegrandruler
 
Posts: 19
Joined: Mon Feb 17, 2014 8:28 am

Re: Macro Error

Postby B Marcelly » Sat Apr 19, 2014 10:44 am

Works for me 8-)
Code: Select all   Expand viewCollapse view
MsgBox( CardCost(1,2,3,4) )

So your problem is because you are omitting some parameters in the call of your function.

The rule is simple :
If a parameter is missing, do not use it !
You are supposing that a missing parameter has a default value. You are wrong.
In OpenOffice Basic, a missing parameter is unusable.

Use a local variable for each optional parameter. Example:
Code: Select all   Expand viewCollapse view
Function CardCost(Optional a as integer, Optional b as integer) as integer
Dim a2 As integer, b2 As integer

if isMissing(a)  then a2 = 0  else a2 = a
if isMissing(b)  then b2 = 0  else b2 = b

CardCost = a2 * b2
end function

The problem is described in Bug 23763 and probably will never be corrected.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Macro Error

Postby mymsman » Sat Jun 07, 2014 7:07 pm

This would appear to be an unfortunate difference between Microsoft and Open Office

I was trying to track down why a spreadsheet was giving different results between Excel and OO calc and it turned to be the original macro writer had depended on being able to depend on being able to set a default value for optional parameters.
Optional mode As String = "m"
In Excel with mode ommitted the code goes through the Case is = 'm' path but in OO goes though Case Else!
Select Case mode
Case Is = "m"
' Convert days to months
getDuration = dDuration / DAYS_PER_YEAR * MONTHS_PER_YEAR
Case Is = "d"
' Return days
getDuration = dDuration
Case Else
' Invalid call. Return error.
getDuration = -1#
End Select

Now I know the problem I can fix the excel source but it is a compatibility issue for spreadsheets intended for naive users.
Bob
OpenOffice 4.1, Win 7 home premium 64bit
mymsman
 
Posts: 5
Joined: Thu Sep 16, 2010 11:47 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests