0

Context: I am trying to create a VSTO add-in for PowerPoint. A function I want to create is for users to click a button -> call an API -> return code that can be executed and carry out changes in powerpoint (e.g. add textbox, formatting).

To do this, I've using CSharpScript.EvaluateAsync and importing the relevant references as below. However, with the sample code below - I am getting the error "error CS0246: The type or namespace 'Shape' could not be found (are you missing a using directive or an assembly reference?)"

private async void submitButton_ClickAsync(object sender, EventArgs e)
    {
        try
            {
                // Call API to get code
                string code = GetCode(UserInput);

                /* As an example, let's say code is as follow 

                string code = @"
                Shape textbox = currentSlide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 100, 100, 200, 50);
                textbox.TextFrame.TextRange.Text = ""generated by code"" ;";

                */

                var scriptOptions = ScriptOptions.Default.WithReferences(typeof(Shape).Assembly).WithImports("Microsoft.Office.Interop.PowerPoint");
                var result = CSharpScript.EvaluateAsync(source, scriptOptions).Result;

            }

        catch (Exception ex)

            {
                MessageBox.Show("Error: " + ex.Message);
            }

    }

I have tried different ScriptOptions but nothing seems to work - would very much appreciate help on where I have made a mistake. Alternatively, happy to take suggestions on other ways I can achieve my objectives above.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
Alex
  • 11
  • 2

0 Answers0