Link to home
Start Free TrialLog in
Avatar of mammouth
mammouth

asked on

Execute String

I need to import email template with VB6.
Template use variable, these variable need to be replace with database content.

Some time there is condition written like this:
<#IF#><##VAR1##>==1<#THEN#>Hello World<#ENDIF#> how are you

I read template and store content of template in string like:
msg = "...<#IF#><##Var1##>==1<#THEN#>Hello World<#ENDIF#> how are you..."

I need to execute this string.
first i will need to replace <#IF#>, <#THEN#>, <#END IF#> with If, Then and End If
and then execute the string to return Hello World only if Var1=1.

Since i have about 150 email templates with +/-10 variable each, i don't want to edit IF manually.
Avatar of alainbryden
alainbryden
Flag of Canada image

here you go:

msg = Replace$(msg, "<#IF#>", "If") 'Get if's out
msg = Replace$(msg, "<##", " ")  'Remove brackets before variable names
msg = Replace$(msg, "##>", " ")  'Remove brackets after variable names
msg = Replace$(msg, "<#THEN#>", " Then" & vbNewLine)
msg = Replace$(msg, "<#ENDIF#>", vbNewLine & "End If")

This will convert the if then else statements into VB code. Then the only thing you need to change is to turn "Hello World" into msgbox "HelloWorld" , at least it seemed to me that was your intention. anyways, you can extract that string as follows (before replacing the code):
 
Dim Result As String
Result = Mid$( msg, InStr(msg, "<#THEN#>") + 1)
Result = Left&( Result, InStr(msg, "<#ENDIF#>") - 1)
Result is now equal to all the text located between the THEN and ENDIF tags, and you can do with it whatever you wish. If there are more than one of these in a single message (which I didn't seem to think there were) then all you have to do is keep track of the InStr position, and look above the last one, or replace the then and endif tags as you go.

Hope that solves all your problems.

Alain
you may want to sub in this for the last one:
msg = Replace$(msg, "<#ENDIF#>", vbNewLine & "End If" & vbNewLine)

and for the first one:
msg = Replace$(msg, "<#IF#>", vbNewLine & "If")

and I didn't realize there were two = signs. These need to be made into one for vB so:
msg = Replace$(msg, "==",  "=")

just so that you get the extra break and you statements end up looking like this:

If VAR1=1 Then
Hello World
End If
how are you...
If ....
Avatar of mammouth
mammouth

ASKER

I have no problem to replace <#IF#> with IF...

My problem is to execute string who content "... IF VAR1=1 THEN Hello World END IF... how are you...".

IF Var1=1, result of execution should be "...Hello world... how are you" and if not "...how are you..."
Ok, i joust found how to resolve my problem.

First: Extract If then

Dim sc As New MSScriptControl.ScriptControl
sc.Language = "vbScript"
sc.ExecuteStatement "IF VAR1=1 Then MyVar=""Hello World"""
Res = sc.Eval("MyVar")

Then concat Res.
Oh : /

When you said "first i will need to replace <#IF#>, <#THEN#>, <#END IF#> with If, Then and End If " I thought you actually still needed to do that. What a lot of work for nothing :P

Hope I was helpful anyways (hint hint ;) )

Alain
well I answered the first half of his question:
1. "first i will need to replace <#IF#>, <#THEN#>, <#END IF#> with If, Then and End If "

just not the second
2. "and then execute the string to return Hello World only if Var1=1."

Any credit due for that?

Alain
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial