Solved

Execute String

Posted on 2006-06-20
9
296 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

828 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