3

I have an interesting issue I've run into where I can easily tap into python code from VB in an excel instance, as described here: Calling python script from excel/vba

However, when I mimic the exact same code structure in visual studio in a form, the pyscript.language = "python" line fails. Does anyone know if MSScriptControl.ScriptControl can be used in VB .Net to control python like you can in excel? This would be a lot easier than setting up a com object for my python scripts.

Example code (need to add microsoft script control 1.0 and excel object library):

Imports Microsoft.Office.Interop

Public Class Form1
    Dim WithEvents PyScript As MSScriptControl.ScriptControl
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlApp As New Excel.Application
        xlApp.Visible = True
        Dim xlwbook As Excel.Workbook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet)
        Dim xlSheet As Excel.Worksheet = CType(xlwbook.Worksheets(1), Excel.Worksheet)

        If PyScript Is Nothing Then
            PyScript = New MSScriptControl.ScriptControl
            PyScript.Language = "python"
            PyScript.AddObject("Sheet", xlSheet)
            PyScript.AllowUI = True
        End If
        PyScript.ExecuteStatement("Sheet.cells(1,1).value='Hello'")


    End Sub
End Class
Community
  • 1
  • 1

1 Answers1

0

According to this http://msdn.microsoft.com/en-us/library/aa227633(v=vs.60) the scriptcontrol is a VS6 item which means that it may not be completely compatible with later versions. There is also an issue of context as the two environments (Windows forms and Excel) are different in fundamental ways so what is possible in one is not always possible in another.

Also it would be well worth double checking the first four steps in the given example and making sure that you can do this from Excel.

Carl Onager
  • 4,112
  • 2
  • 38
  • 66