2

I want to call a routine with arguments to be used as the condition of a loop. The code that follows doesn't works, it gives the ERROR 13 (incompatible types). How do I fix this? Many thanks!

Sub foo()
   Call bar("a>10")
End Sub

Sub bar(myCondition as String)
   Dim a as Integer
   Do
      Debug.Print a
      a=a+1
   Loop Until myCondition
End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
sergio trajano
  • 189
  • 1
  • 1
  • 14

1 Answers1

1

Word has no equivalent to Excel's Evaluate so this is only possible the "long way around". You need to figure out every variation you want to pass for evaluation, test for which it is, and then run the correct type of loop. Seems to me you'd need to do something like this, anyway, since using the test you have in your example wouldn't loop for "<" (assuming you use positive integers).

In order to avoid repeating the code you want to execute in the loop, put that in a separate function.

Sub foo()
   Call bar(">", 10)
End Sub

Sub bar(ByVal conditionOperator As String, ByVal conditionValue As Long)
   Dim a As Long
   Select Case conditionOperator
    Case Is = "<"
        Do
         a = PerformLoop(a)
        Loop While a < conditionValue
    Case Is = ">"
        Do
         a = PerformLoop(a)
        Loop Until a > conditionValue
    Case Is = "="
        Do
         a = PerformLoop(a)
        Loop Until a = conditionValue
    Case Is = ">="
        Do
         a = PerformLoop(a)
        Loop Until a >= conditionValue
    Case Is = "<="
        Do
         a = PerformLoop(a)
        Loop While a < conditionValue
   End Select
End Sub

Function PerformLoop(a As Long) As Long
      Debug.Print a
      a = a + 1
      PerformLoop = a
End Function
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Thank you @Cindy Meister! Very clever code! But actually what I want do to is find paragraphs in a text that have a given font color. I am afraid for each color I will have to write a whole subroutine, so I am going to repeat myself a lot :(. I would like to do something like FindNextParagraph("Green"), FindNextParagraph("Red"), FindNextParagraph("Blue"). But I am afraid I have to do FindNextParagraphGreen(), FindNextParagraphRed(), FindNextParagraphBlue(), and so on. Any idea? – sergio trajano Feb 22 '18 at 14:56
  • @sergiotrajano that is a totally different question. Cindy answered the question you asked. This should be marked as correct and you need to ask a question that actually refers to the problem you want. – Scott Craner Feb 22 '18 at 14:59
  • 1
    @sergiotrajano Too bad you didn't actually ask the question you want answered... If you mean exactly that what you want is Word's advanced FIND feature. You can use the tools under the "More" button to search for formatting. Play with that a bit, then record those steps in a macro. I believe you'll see that you can use values you *can* pass to a function. If you need help with it, post a new question, please :-) – Cindy Meister Feb 22 '18 at 15:00
  • I am very sorry for my mistake @ScottCraner. I tried to formulate a generic question concerning the do-loop-until condition, that is what a am using in my code: Loop Until Selection.Font.ColorIndex = wdRed. I am so sorry guys, my bad :( – sergio trajano Feb 22 '18 at 15:07
  • 1
    Well @sergiotrajano take a close look at the code you just posted: wdRed is not a string, it's a constant value with a Long data type equivalent. You can pass that any time... – Cindy Meister Feb 22 '18 at 15:09
  • @CindyMeister Sure I want to thank you! If fact, I should to pay you. I did mark to green the checkmark, but the upvote that I did too do not apears publicitly because I am under 15 reputation, sorry for that. – sergio trajano Feb 22 '18 at 17:03
  • No problem @sergiotrajano :-) Enjoy making Word jump through hoops! – Cindy Meister Feb 22 '18 at 17:27