Calling Application.Run with built in VBA function as Macro Parameter

ttt123

Board Regular
Joined
May 31, 2006
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hello, I am hoping that someone will be able to help me with a problem that I am having for my COM addin.

My Problem:
I would like to be able to call VBA's Format from my C# VSTO application, but this function is part of the VBA language and is not exposed via COM. Note that I am aware that .NET has a string.Format() function and that a similar function ExcelApp.WorksheetFunction.Text() exists that can be called via COM, but I would really like to be able to call the Format function in VBA since it takes different format strings than .NET's string.Format and has slightly different outputs than WorksheetFunction.Text.

Proposed Solution:
One way I could solve this is to call a VBA wrapper function that exists in a helper .xlam file from my C# code via Application.Run().

//VBA wrapper function in addin
Code:
Public Function VBAFormat(v As Variant, s As String) As String
    VBAFormat = Format(v, s)
End Function

//sample C# code
Code:
string val = "2005/01/01";
string format = "[$-C0C]d mmm yyyy;@"; //A French Canadian locale specific format string
MessageBox.Show(Globals.ThisAddin.Application.Run("MyHelperAddin.VBAFormat", val, format));

This works fine, but would require me to ship an xlam file along with my COM addin and I would also have to ensure that it was installed and deal with macro security for it to work. I would prefer to call the Format function directly without a wrapper, but I haven't had any luck doing that.

if you try this in the VBE intermediate window, you will get an error with this VBA code: ? Application.Run("Format", 1, "0.00")

Is there a fully qualified string that I can use for the Format function? I tried VBA.Strings.Format to no success.

Does anyone know of any other way I could access the Format function in a COM addin?
One thing I was looking at but havent had any success with are various apps that I found via Google that claim to be able to compile VBA functions into a DLL file which I was hoping I could reference into my VS project. However, I have not had any success getting my function converted to a dll file with any of these apps.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top