VB standard functions 'Name X is not declared'

May 27, 2013 at 11:50 AM

I have some working VB that I'm trying to put into Excel-DNA, but having some compilation problems.

The first is that referencing built in functions like "Time" generates an error "Name 'Time' is not declared"

This code works in Excel VB, but in Excel-DNA the second line generates the error.
Dim Time1
Time1 = Time
Any help appreciated, thanks.
May 27, 2013 at 12:06 PM
You can try something like:

Dim d As DateTime = Now

The built-in VBA functions and properties like these are not part of the language in VB.NET, and so need to be provided by libraries. Many of the VBA functions are exported from the Microsoft.VisualBasic and Microsoft.VisualBasic.Compatibility assemblies that are referenced by default. For example your call to MsgBox is really a call to "Microsoft.VisualBasic.Interaction.MsgBox" in the Microsoft.VisualBasic assembly.

The "Now" will refer to "Microsoft.VisualBasic.DateAndTime.Now", which just returns "System.DateTime.Now". (The difference has to do with how the types are made available to VB.NET without qualification - so you can just say "Now" and VB.NET will pick it up from "Microsoft.VisualBasic.DateAndTime.Now".

If you have other missing ones, you're welcome to post back.

May 28, 2013 at 12:03 AM
Edited May 28, 2013 at 12:03 AM
Thanks Govert, that helped a lot.

I've gone to msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime.aspx and found the names of functions that will work, and decided that Timer works best for my situation. It looks like I'll be able to get more function options from there as well.