Solved

Execute String

Posted on 2006-06-20
9
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:mammouth
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now