I wrote this code to automate in Word from Visual Fox Pro. I put between the escapes <|<
and >|>
some VFP code (like take some values from tables and so on). At the press of a button, the code scans all of the document and, when finding these escapes, evaluates the instructions between. The last two FOR
loops eliminate the escape strings.
The code works, but I ask. Is there a better way to do what I look for?
LOCAL oWord, oDocument, oRange, Lc_start, Lc_expr, Lc_ris, Ln_count, Lc_val
DIMENSION Lc_funct[1]
Lc_expr = ""
Ln_count = 1
oWord = CREATEOBJECT("Word.Application")
oWord.Visible = .T.
cFile = GETFILE()
oDoc = oWord.Documents.Add(m.cFile)
oRange = oWord.ActiveDocument.Range()
nWordCount = oRange.Words.Count
oRange.Collapse(1)
FOR nWord = 1 TO m.nWordCount-1
oRange.Expand(2)
cWord = oRange.Text
DO CASE
CASE m.cWord = "<|<"
Lc_start = .T.
Lc_Expr = ""
CASE m.cWord = ">|>"
Lc_start = .F.
Lc_funct[Ln_count] = Lc_Expr
Ln_count = Ln_count + 1
DIMENSION Lc_funct[Ln_count]
CASE m.Lc_start
Lc_expr = Lc_expr + m.cWord
OTHERWISE
*do anything
ENDCASE
oRange.Collapse(0)
ENDFOR
FOR i = 1 TO Ln_count-1
oRange = oWord.ActiveDocument.Range()
oRange.Find.Text = Lc_funct[i]
oRange.Find.Replacement.Text = EVALUATE(Lc_funct[i])
lFound = oRange.Find.Execute( , , , , , , , , , , 2 )
ENDFOR
FOR i = 1 TO Ln_count-1
oRange = oWord.ActiveDocument.Range()
oRange.Find.Text = "<|<"
oRange.Find.Replacement.Text = ""
lFound = oRange.Find.Execute( , , , , , , , , , , 2 )
ENDFOR
FOR i = 1 TO Ln_count-1
oRange = oWord.ActiveDocument.Range()
oRange.Find.Text = ">|>"
oRange.Find.Replacement.Text = ""
lFound = oRange.Find.Execute( , , , , , , , , , , 2 )
ENDFOR
MESSAGEBOX("DONE")
DIMENSION
orOTHERWISE
and there's noSet
keyword used || Oh I see,VFP
is a programming language \$\endgroup\$Range.Find
method, it's a lot more powerful than just find/replace. For starters, there's no need to loop in your last 2 find/replaces, since you usewdReplaceAll
, it should remove all matches. In fact you can narrow it down further, with some wildcards:oRange.Find.Execute FindText:= "[\<\>]|[\<\>]", MatchWildcards:= True, ReplaceWith:= "", Replace:= 2
. That will catch<|<
or>|>
. You can probably eliminate looping altogether with some judicious.Find
. (NB assuming VFP supports named arguments) \$\endgroup\$