Using If Statements

 

The If statements add a new level of sophistication and intelligence to macros. They allow macros to take action based on certain conditions. If the condition is met, the macro takes a specific course of action. If it is not met, the macro does something else or ignores the condition. For example, insert text into a window if the window is open. If it is not open, have the macro do something else.

 

Below are some examples and things to keep in mind when using If statements.

 

 

1. End If

Every If statement must have a closing End If statement. The End If command signifies the end of the If statement and conditional logic used in the macro.

 

This sample macro highlights a line of text in Notepad and copies it to the clipboard. If the clipboard contains the word "Computer", the line is pasted into Excel. If the line does not contain the word "Computer", then the conditional is ignored and the next line in Notepad is copied to the clipboard.

 

 

Sample Macro

 

Repeat Start (Repeat 150 times)

  Text Type (Simulate Keystrokes): <SHIFT><END>

  Clipboard Copy

  Delay: .25 seconds, without ability to halt

  If Clipboard Contains "Computer"

    Window Activate: Excel

    Wait for Window Title: Excel

    Clipboard Paste

    Delay: .25 seconds, without ability to halt

    Text Type (Simulate Keystrokes): <ENTER>

    Window Activate: notepad

  End If

  Text Type (Simulate Keystrokes): <ARROW DOWN><HOME>

End Repeat

 

 

2. Else

The Else statement is the primary way to provide an either/or type option. If one condition isn't met, then the macro takes an alternative course of action. It does something else.

 

This sample macro is similar to the one above but includes an Else statement. The macro highlights a line of text in Microsoft Word and copies it to the clipboard. If the clipboard contains the word "Computer", the text is pasted into Notepad. Because of the Else statement, the text is pasted into WordPad if the clipboard does not contain the word "Computer".

 

 

Sample Macro

 

Repeat Start (Repeat 10 times)

  Text Type (Simulate Keystrokes): <SHIFT><END>

  Clipboard Copy

  Delay: .25 seconds, without ability to halt

  If Clipboard Contains "Computer"

    Window Activate: notepad

    Wait for Window Title: notepad

    Clipboard Paste

    Delay: .25 seconds, without ability to halt

    Text Type (Simulate Keystrokes): <ENTER>

    Window Activate: Microsoft Word

  Else

    Window Activate: Document - WordPad

    Clipboard Paste

    Delay: .25 seconds, without ability to halt

    Text Type (Simulate Keystrokes): <ENTER>

    Window Activate: Microsoft Word

  End If

  Text Type (Simulate Keystrokes): <ARROW DOWN><HOME>

End Repeat

 

 

3. AND/OR/XOR

The AND/OR/XOR options provide additional If Statement flexibility. The AND option requires that two If statements be true before action is taken. The OR option requires that either one or the other If statement be true before action is taken. With the XOR option, one If statement must be true and the other false in order for subsequent action to be taken.

 

Example macros using these options are included in the AND/OR/XOR help topic.

 

 

4. Nested If Statements

If statements may be nested inside other If statements. This example is a segment of a larger macro. It illustrates the nesting of one If statement within another. Each of the nested If statements requires a matching End If command.

 

 

Sample Macro

 

Variable Modify String %T[15]%: Copy Whole Text (%N[1]%)

  If Variable %N[1]% Is Greater Than 1

    Variable Modify String: Replace "(" in %T[16]% with ""

    Variable Modify String: Replace ")" in %T[16]% with ""

  Else

    If Variable %N[1]% Equals 1

      Variable Modify String: Replace "(s)" in %T[16]% with ""

    End If

  End If