Solved

Execute String

Posted on 2006-06-20
9
299 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

687 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