Provides an add-in that executes VB.NET script in Excel formulas.
Build with .NET 6 Windows Desktop SDK or the latest Visual Studio 2022. Tested With Visual Studio 2022 version 17.6.5
.
- Set value of A1 to the following value:
Dim randomId = Guid.NewGuid
Return randomId.ToString
- Set formula of B1 to the following value:
=VB.NET.FUNCTION(A1)
Press Enter to run the formula function. It generates a random GUID.
- Set value of A1 to the following value:
Return Regex.Replace(lookIn,findWhat,replacement)
- Set value of A2 to the following value:
Excel can run Python.
- Set value of A3 to the following value:
(?<=run )\w+
- Set value of A4 to the following value:
VB.NET
- Set formula of A5 to the following value:
=VB.NET.FUNCTION(A1, "lookIn", A2, "findWhat", A3, "replacement", A4)
Press Enter to run the formula function. It returns Excel can run VB.NET.
.
- Set value of A1 to the following value:
Dim systemFolder = Environ("WinDir")
Dim winIniPath = Path.Combine(systemFolder, "win.ini")
Return Await File.ReadAllTextAsync(winIniPath)
- Select B1 and enable text wrapping
- Set formula of B1 to the following value:
=VB.NET.ASYNC.FUNCTION(A1)
Press Enter to run the formula function. It reads all text of win.ini
asynchronously.
' VB default
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks
' Added some additional imports based on Excel built-in functions
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Text.Json.Nodes
Imports System.Numerics
Option Compare Binary
Option Strict Off
Option Explicit On
Option Infer On
.NET 6.0 for Windows Desktop
Assemblies of the VB console app workload will be referenced by your script.
We plan to add all assemblies in the Windows Desktop SDK with some special syntaxes or settings.
The VB used by this plugin is a scripting dialect.
Your code will be splitted into 2 parts, then transform into a regular VB source snippet for running.
- Options and imports.
- Function body.
For example, in the following code, Option Strict Off
and Imports System.Net.NetworkInformation
are in the first part. Other lines are in the second part.
Option Strict Off
Imports System.Net.NetworkInformation
Dim pingSender As New Ping
Dim options As New PingOptions
' Set options to include the IP address and round trip time
options.DontFragment = True
' Create a buffer of 32 bytes of data to be transmitted
Dim buffer(31) As Byte
' Send the ping request
Dim reply As PingReply = Await pingSender.SendAsync(hostName, 120, buffer, options)
' Check if the ping was successful
If reply.Status = IPStatus.Success Then
Return "Ping successful. RoundTrip time: " & reply.RoundtripTime
Else
Return "Ping failed. Error message: " & reply.Status.ToString()
End If
The first part will be placed at the beginning of the template.
The second part will become the body of a function. The function will be invoked later.
Your script can accept parameters passed by callers. To reduce compilation time, each parameter is an Object
. You'll need to convert them to your expected types before using them.
Important: This feature is not implemented at this time.
Adds assembly references in your code snippet.
For example, #R Assembly "WindowsBase.dll"
will search for the WindowsBase.dll
in the current assembly load context and let your code snippet reference it.
Adds a VB source file to the project of your code snippet.
For example, #R Source "C:\Program.vb"
will add C:\Program.vb
to your code snippet's project.
Adds a NuGet package to the project of your code snippet.
For example, #R NuGet "System.Runtime.CompilerServices.Unsafe"
will add NuGet package System.Runtime.CompilerServices.Unsafe
to your code snippet's project.
This feature is used to reference Windows Desktop assemblies.
#R Workload "Windows Forms"
references Windows Forms.#R Workload "WPF"
references Windows Presentation Foundation.- WinUI is not supported, because it requires adding dynamically compiled Windows RT resources and source generators to your script.
It must be placed before any Option
or Imports
statements.
We cache up to 100
assemblies for each different script.
When the cache is full, it removes the least recently used script and unloads the assembly load context.
Based on this strategy, we recommend using parameters in your script instead of dynamically generating code.
The current implementation of special syntaxes are not based on VB language features. It's based on text transformation in formula functions. Although it supports the Imports
statement at the beginning of the code, it could have unexpected behavior when the code contains preprocessor directives before Imports
statements. Line numbers and text spans of the source code are also broken.
Resolution: Switch to ModVB when it supports these syntaxes.