我遇到了一个有趣的问题,我可以很容易地在excel实例中使用VB中的python代码,如下所述:Calling python script from excel/vba
但是,当我在窗体中模拟VisualStudio中完全相同的代码结构时pyscript.language=“python”行失败。有人知道吗MSScriptControl.ScriptControl是否可以像在excel中一样在VB.Net中使用来控制python?这比为python脚本设置com对象要容易得多。在
示例代码(需要添加microsoft script control 1.0和excel对象库):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