dynamic vbscript execution

Anandhan Sathyanarayanan 136 Reputation points
2021-01-20T16:42:16.71+00:00

Hi,

My requirement is to execute the vbscript from C# (not from file). Followed the below article and tried to execute a simple statement WScript.Echo "Missing parameters but it's always giving syntax error as mentioned in the below article.

https://stackoverflow.com/questions/42632185/how-to-format-vbscript-string-executed-dynamically-through-c-sharp

Please guide to execute vbscript from C#

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,201 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2021-01-20T20:02:55.477+00:00

    This test works fine for me =>

    var scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
    dynamic objScriptControl = Activator.CreateInstance(scriptType);
    objScriptControl.Language = "vbscript";
    string sScript = "function Myfunction()" + Environment.NewLine +
        "Dim Text, Button, Title, Result" + Environment.NewLine +
         "Text = \"Are you OK ?\"" + Environment.NewLine +
           "Button = vbYesNoCancel+ vbQuestion" + Environment.NewLine +
            "Title = \"Question\"" + Environment.NewLine +
              "Result = MsgBox(Text, Button, Title) " + Environment.NewLine +
    "end function" + Environment.NewLine;
    objScriptControl.AddCode(sScript);
    objScriptControl.Eval("Myfunction()");
    

0 additional answers

Sort by: Most helpful