?
Solved

Execute String

Posted on 2006-06-20
9
Medium Priority
?
300 Views
Last Modified: 2010-04-07
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.
0
Comment
Question by:mammouth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
9 Comments
 
LVL 20

Expert Comment

by:alainbryden
ID: 16943349
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
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 16943380
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 ....
0
 
LVL 1

Author Comment

by:mammouth
ID: 16943415
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..."
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:mammouth
ID: 16943450
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.
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 16943605
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
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 17308203
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
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17338125
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question