OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Macro Basic: "If" statement problem or is it the &

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Mar 24, 2004 2:50 pm    Post subject: Macro Basic: "If" statement problem or is it the & Reply with quote

I am getting a syntax error when I use a cascading IF, but as far as I can ascertain (from the Help and Sun's Basic Programming manual) this should at least run (I'm not sure about the logic of what I am trying to achieve!):

if (charCount > 0) and (len(numberToRemoveAsString)= 1) then outputString = left(inputString, charCount - len(numberToRemoveAsString) - 1 ) + right(inputString, len(inputString)-charCount)
oSelection.String = outputString
else if resultofand = -1 then outputString = left(inputString, charCount - 2 ) + right(inputString, len(inputString)- charCount)
oSelection.String = outputString
else MsgBox "That number is not present"
end if

This produces an error at the elseif: "else/elseif without if"
Also, I have to click the OK button twice to dismiss this message.
I have another version of this which does not produce a syntax error but also does not produce the correct results - the actual logic of the processing of the IFs is not what I would expect. It seems that only the first test is done, if that fails the macro continues after the "endif" and ignores the "elseif".
Why do none of the help examples include more than one line of code to be executed after an "if" or "elseif"
Back to top
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Wed Mar 24, 2004 3:28 pm    Post subject: Reply with quote

See if this works for you:
Code:
if (charCount > 0 and len(numberToRemoveAsString)= 1) then
  outputString = left(inputString, charCount - len(numberToRemoveAsString) - 1 ) + right(inputString, len(inputString)-charCount)
  oSelection.String = outputString
 elseif resultofand = -1 then
  outputString = left(inputString, charCount - 2 ) + right(inputString, len(inputString)- charCount)
  oSelection.String = outputString
 else MsgBox "That number is not present"
end if
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Mar 25, 2004 2:20 pm    Post subject: IF Reply with quote

Thanks John V - that at least solved the syntax error and probably explained why I thought the "if" command did not work as expected. I feel really stupid but in my defence I have used languages that don't mind where you put the rest of the "then". Do you think there is a case to be made for the manual/help page to mention explicitly that the "then" statements should begin on a separate line?
Thanks again from the other side of the Atlantic.
Back to top
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Thu Mar 25, 2004 3:53 pm    Post subject: Reply with quote

Didn't Will Rogers say something like, "I never met a language that didn't have its syntax quirks."?

You can get away with continuing "then" on the same line if you use a colon or if you have a one line "If" statement.
Code:
Sub Main
a = 1
If (a = 1) then : b=1
 ElseIf (a = 2) then : b = 2
 Else b = 3
EndIf
If (true) then print b
End Sub

Also note that it appears "ElseIf" needs to be one word, at least in this context. If you make it 2 words here you get an error.
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Mar 27, 2004 1:26 pm    Post subject: More on Basic Reply with quote

Can't comment on Will Rogers - he's no that well known over here. Back on subject, how did you find out that you can continue "then" on the same line. I didn't see it in the Sun manual. Is it a guess from using a different language or is there more/better documentation out there?
Back to top
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Sat Mar 27, 2004 6:33 pm    Post subject: Reply with quote

I found a log of things that are not documented. Some of them I found by reading the source code, some I found by looking at other examples, and some I found because it worked in VB, I tried it in OOo, and it worked.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Sun Mar 28, 2004 9:15 am    Post subject: Reply with quote

Quote:
how did you find out that you can continue "then" on the same line
When I get syntax errors I just try things to get rid of them. Because ":" is a command separator I tried that.

It's not easy to read code when more than one command exists on a line but it sure can make it shorter so I often code in that manner, especially if it just something simple.

Will Rogers was a famous American humorist ("down home" and folksy). His most often quote line is, "I never met a man I didn't like." or something close to that.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group