1

I have an Excel file that contains public constants in a user-created module, and I need to read those constants from a C# application.

In the Excel VBA I have in a module called CellDefinitions e.g.:

Public Const PROJECT_ID_RANGE As String = "A18"

Here is what I have tried from C#, but it obviously doesn't work, since constants are not macros:

var excel = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = excel.Workbooks.Add(excelTemplateFile);
var value = excel.Run("CellDefinitions.PROJECT_ID_RANGE");

The Excel file has the constants also as properties, but they cannot be read from C#, since they are located in ThisWorkbook module (see Access return value from VBA Function in .NET?).

Note that I cannot edit the Excel file or its VBA code (to e.g. create new functions in a new module), so I'm looking for a solution only for the C# part.

Jouni K
  • 11
  • 1
  • 2
  • Off the top of my head I'm not sure, never done it this way round before. Not sure you can access an unknown constant like that because it doesn't know what the constant is until the book is loaded? It's not like a class or library you're loading... You could always build a function in Excel that returns any given constant value. – jamheadart Jul 07 '20 at 10:27
  • Also are you sure they're located in `ThisWorkbook` module and not just a `module` ? I didn't think you could have constants in `ThisWorkbook` – jamheadart Jul 07 '20 at 10:43
  • You may be better copying the global constants to workbook.customdocumentproperties. – freeflow Jul 07 '20 at 12:36

0 Answers0