Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluating code/expressions at runtime

Status
Not open for further replies.

tommyboyau

Programmer
Feb 7, 2005
47
0
0
AU
Hey guys,
I will try and explain this as best as i can.
My program is going to look at word templates and replacing certain values inside them with values from a database.
Some elements within this word document will need to be a bit more complex then finding and replacing such as IF statements.
Something like
If dbClientStatus = "New" Then "Welcome"

Imagine that single line is in a word document, i want to be able to parse that statement or have something similar where it evaluates it and if it's true it outputs a value.
I'm not sure how I should tackle this and was hoping for some hints.
Ask any questions and I will provide as much detail as possible.

Thanks,

Tom
 
It sounds like you're looking for something like this:
Code:
Dim var As String
var = IIf(dbClientStatus = "New", "Welcome", "Not New")

Hope that helps.
 
I would recommend looking into word Merge Letters. Merge letters allow you to pull data from a database or flat file and insert it into a word document (template). You can also use some basic opperators in word, including IF statements.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I agree with ThatRickGuy - Word has functionality that can do this. Why reinvent the wheel?

That said, there is a way to evaluate text as code, using the Microsoft Script Control COM library. To do this:

Add a reference to 'Microsoft Script Control'


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Create and instantiate a script control
Dim sc As MSScriptControl.ScriptControlClass

sc= New MSScriptControl.ScriptControlClass

'set the scripting language
sc.Language = "VBScript"

sc.Eval("MsgBox(""Hello World!"")")

sc= Nothing

End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
ThatRickGuy - I've already got the basic VB.Net and Word functionality down pat. Like i can merge the details from the DB into the letter that works and thats cool.
I'm investigating several methods such as XML or simple find and replace routines.

Jebenson, thanks for your code. I will take a look and return to let you guys know how i got along!

Thanks for the prompt replies - You guys at tek-tips never cease to amaze me!
 
ThatRickGuy - Was what jebenson posted what you had in mind?
If there is another way you were thinking of I would be interested to hear it. Perhaps there is an article or example you could point me towards?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top