Jump to content
Macro Express Forums

If Statement


Recommended Posts

I have a line of text that I am copying to Variable %T[90]%. I have a few different scenarios that I need to check that line of text for in order to run the proper macro. I am processing a text file into variables but I need to determine which line I am on within the text file, in order to run the proper macro against that line.

 

I though and IF state would be best. I am familiar with If Statements but somewhere must be missing something.

 

If my variable contains the text:

 

"BLD Info Update" I want to run Macro - Builder

"SqFt" I want to run Macro - Model

"Picture" I want to run Macro - Picture

"Hours" I want to run Macro - Hours

 

I have tried so many different ways and I am just not getting it right. Can't I say:

 

If contains...

run macro a

or

if contains...

run macro b

or

if contains...

run macro c

 

I was running into a problem with my End Ifs.

 

I just can't quite see what I am doing wrong.

 

Any help is always appreciated.

 

Heather

Link to comment
Share on other sites

You're on the right track but assuming there's one macro per condition you want something like this below.

 

If contains Ted

Run Macro A

Endif

 

If contains Fred

Run Macro B

Endif

 

If contains Tom

Run Macro C

Endif

 

Caveat being if the text being compared matches multiple If Contains. Otherwise you need to nest the condition with Elses.

 

Also you might want to consider using Switch Case. It often is more elegant and flexible

Link to comment
Share on other sites

The text that it contains is unique to the line. I did check out the Switch Case, but I got the understanding it wouldn't work with a "contains" situation but rather an exact equal situation...I could be wrong. (Its been known to happen from time to time...lol)

 

I also need a Macro Return at the bottom of each of the Macros that I am calling right?

 

Also how would be the best way to repeat the Macro?

 

Thanks again Cory!!!

Link to comment
Share on other sites

You could use nested if-else statements, but these get complicated fast...

 

If %X% contains "aaa"
 // Do this
 Else
   If %X% contains "bbb"
   // Do that
  Else
  If %X% contains "ccc"
  // Do something else
  End if
   End if
End if
// Do this always

 

Not sure whether or not you need to use the Macro Return. I don't think I have ever used it. Experiment with and without it. But I have found that including a Macro Stop statement in a called macro halts the calling macro, so be careful!

Link to comment
Share on other sites

I think you're correct about Switch Case and If Contains. In that case my example stands. If unique you do not need to nest which makes it a lot easier to read.

Link to comment
Share on other sites

Cory,

 

This is the Macro I am using. Now I am having a different problem. The text is not being saved into the Variable. I have tried every which way. The text is in the buffer or clipboard but is not being carried into the Variable. I don't think I have done anything wrong but...

 

I tested up to Line 7 and its not working that far or at least the copy to variable which throws off the rest.

 

Can you see what I am doing wrong??

 

 

Window Activate: work - Boxer Text Editor - *
Repeat Until %T[90]% Contains "STOP HERE"
Clipboard Empty
Text Type (Simulate Keystrokes): <SHIFT><END>
Clipboard Copy
Delay: 465 milliseconds
Variable Set String %T[90]% from the clipboard contents // Test for Value
If Variable %T[90]% Contains "BLD Info Update"
 Macro Run: Builder // Run Macro for BUILDER
Else
 If Variable %T[90]% Contains "SqFt"
   Macro Run: Model // Run Macro for MODEL
 Else
   If Variable %T[90]% Contains "Picture"
     Macro Run: Picture // Run Macro for PICTURE
   Else
     If Variable %T[90]% Contains "Hours-"
       Macro Run: Hours // Run Macro for HOURS
     Else
       If Variable %T[90]% Contains "STOP HERE"
       End If
     End If
   End If
 End If
End If
Text Type (Simulate Keystrokes): <ARROW DOWN><HOME>
End Repeat

Link to comment
Share on other sites

It sometimes takes time for a variable to receive data from the clipboard. Add a delay after the clipboard copy.

 

Add a delay after this line.

Variable Set String %T[90]% from the clipboard contents // Test for Value

 

When debugging macros, long delays are your friends. Add delays liberally; you can always remove them later.

Link to comment
Share on other sites

Alan,

 

Thanks for the help. I was able to get it to run through. I am still not exactly sure what was causing the problem with getting the text from the clipboard to the variable, but it is working. I am getting occasional problems but I think once I add a few delays all will be fine.

 

Thanks again for the help.

 

Heather

Link to comment
Share on other sites

Heather you don't need to nest. You can do like in my sample. Each works it's just that nested is a little uglier.

 

The clipboard commands take time I believe because MEP is doing some checking and processing. And sometimes the delay is considerable and sometimes very quick. If I want my code to run fast I create a loop with a 100mS delay and set the variable to the clipboard contents. Then I add a condition that when the variable does not equal nothing to break out of the loop. That way if it's working quick I only pay a 100mS penalty. But in the rare occasion that it needs more time it still is accommodated.

 

Sometimes it's better to use CTRL+C instead of the MEP Clipboard Copy command. Always quicker and I don't think I have ever seen a negative side effect.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...