# Visual Fox Pro Automation

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")

• Is this real VBA or something closely related like vbscript? Don't recognise DIMENSION or OTHERWISE and there's no Set keyword used || Oh I see, VFP is a programming language – Greedo May 26 '19 at 8:20
• Cross-posted on Stack Overflow – Mast May 26 '19 at 11:28
• I recommend reading up on the 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 use wdReplaceAll, 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) – Greedo May 26 '19 at 17:35
• Where I can find some good documentation about word automation? there is something, but I need more specific analysis... By the way, thank you for your time, will try to implement this modifications... – meltigel May 27 '19 at 7:46