Home Image-Pro Automation (Macros, Apps, Reports)

more simple questions


I tried to write a very simple script which I thought would work:

for i = 1 to 2
msgbox "here"
next i

This went into an infinite loop and I had end imagepro (alt-ctrl-del), delete the project folder and start again :(

1) why did the code fail?
2) In future if there is a run time error what is the recommended approach to stopping the code?

Apologies for this simple query

Best Answer

  • edited August 2014 Answer ✓
    When your macro type is SimpleScript (created by macro recording) it's get executed right when the project is loaded (to assign all variables), so you have to use CodeCommand to insert your own code (drop it to the macro from the Toolbox) and add the code there, like this:

        Public Function MySimpleMacro() As SimpleScript
            MySimpleMacro = New SimpleScript
    
            With Automate.ScriptingCommands.CodeCommand(MySimpleMacro)
                If .Run() Then
                    ' User Code Here
                    For i As Integer = 1 To 2
                       MsgBox "here" & i
                    Next i
                End If
            End With
    
        End Function
    
    CodeCommand ensures that your code is executed only when the macro runs.

    Another way is to use just normal subs or functions:

        Sub MySub
            For i As Integer = 1 To 2
               MsgBox "here" & i
            Next i
        End Sub
    

    Regards,

    Yuri
Sign In or Register to comment.