Execute VBScript on DIAdem Launched by Python using Active X

Updated Jul 21, 2021

Environment

Software

  • DIAdem

Other

  • Visual Studio Code

DIAdem can be operated from other programing languages via Active X. Here, we prepare a Python environment (Visual Studio Code) to create and execute Python code. You can check the procedure for starting DIAdem and executing VBScript. DIAdem with a valid license is required for this procedure.

Installation of Python IDE

1. Python has multiple types of IDEs (Integrated Development Environments) such as PyCharm, Visual Studio Code, and Visual Studio Community. Here we use Visual Studio Code. Visual Studio Code is a source code editor developed by Microsoft. After the installation is complete, start Visual Studio Code and install Python and Pylance. It is important that Python, pip, and Pylance are installed during installation. You need to install Python and Pylance manually while pip will be install with Python 3.4 or later automatically.
 
1installPylance2.png

2. Install Pywin32 by use of pip. Pywin32 is an additional library that allows you to call Windows APIs from Python. Here, a library called win32com.client included in Pywin32 is used to create a COM (Component Object Model) client or server. It is a library that is also needed when using Active X to control Excel and Word, and is frequently used. When installing Pywin32, start the command prompt with administrator privileges and start the installation with the following command.
py -m pip install pywin32
2InstallPyWin32.png


Run Python Code on Your Environment

Firstly, let's create a simple *.py file and check that python can be executed as expected.

1. After launching Visual Studio Code, select New File from File to create a new file. Explorer allows you to see currently open files and files on your desktop. There are multiple programming languages available on Visual Studio Code, so first save this file as *.py. Here, save it as hello.py.
3New.png

2. Enter the following code in hello.py. If you press the execute button on the upper right, you will get the response hello, python as shown below.
print("hello, python")

4writeandrun2.png

3. (Optional) If you have Excel and can be controlled by ActiveX, you can start Excel from Python by creating the following Python code. You have to put valid file path for "C:\Users\user\Desktop\test.xls". It will create a *.xls file and write a text on the selected cell. 
import win32com.client
Excel = win32com.client.Dispatch("Excel.Application")
Excel.Visible=True
ExcelWorkbook = Excel.Workbooks.Add()
ExcelWorkbook.SaveAs(r"C:\Users\user\Desktop\test.xls")
ExcelWorkbook.ActiveSheet.Cells(1, 1).Value = "hello, excel"   
print(str(ExcelWorkbook.ActiveSheet.Cells(1,1)))
ExcelWorkbook.Close(SaveChanges=True) 
Excel.Quit()

OpenExcel.png

If this script works, you can use Active X through Pywin32 on your environment.


 

Launch DIAdem from Python

1. To create a DIAdem COM by win32com.client of Pywin32, you need following codes. You can control DIAdem from Python via Active X by creating a new *.py file and enter the code below.

import win32com.client
DIAdem = win32com.client.Dispatch("DIAdem.ToCommand")
DIAdem.bDontCloseApplication = True
DIAdem.CmdExecuteSync("wndshow('shell','Minimize')")
DIAdem.CmdExecuteSync(r"Scriptstart('C:\Users\username\Desktop\Msgbox.vbs')")

 

2. The VBScript file selected by 'C:\Users\username\Desktop\Msgbox.vbs' is executed on DIAdem. You can create Msgbox.vbs by Notepad by saving the following text in Notepad as Msgbox.vbs. When you double click Msgbox.vbs, you wil see a message box with the text.

MsgBox("This is DIAdem Script.")


3. After creating Msgbox.vbs, you need to adjust the file path of C:\Users\username\Desktop\Msgbox.vbs in the python code.

4. Executing Python, DIAdem will be started, Msgbox.vbs will be executed. The message box will be displayed.

5RunDIAdemScript2.png