How to execute an Excel macro using Silk Test Workbench .NET Script

How to execute an Excel macro using Silk Test Workbench .NET Script

This solution requires native VB.NET and does not utilize any Silk Test API. To execute an Excel Macro in Workbench, you will need to use the Microsoft Interop libraries.

Please see the example below. This will execute a Macro against an Excel workbook.

Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop


Public Module Main
    Dim _desktop As Desktop = Agent.Desktop

    Public Sub Main()
        
        RunExcelMacro("C:\YourExcelBook.xlsm", "YourMacroName")
        
    End Sub

    
    Private Sub RunExcelMacro(ByVal Path As String, ByVal MacroName As String)
        
        Dim oExcel As Excel.Application
        Dim oBook As Excel.Workbook
        Dim oBooks As Excel.Workbooks
        oExcel = CreateObject("Excel.Application")
        oExcel.Visible = True
        oBooks = oExcel.Workbooks
        oBook = oBooks.Open(Path)
        oExcel.Run(MacroName)
        oBook.Close()
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
            
        MsgBox("Macro has ran", MsgBoxStyle.Information)
        
        End Sub    
    
End Module

You will need to add a reference to the Microsoft.Office.Interop.Excel dll to your .NET script.

 

When you execute the script, it should now run the desired Macro within your Excel workbook.

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2018-02-21 09:05
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.