Using VBA’s Date and Time Functions (Creating VBA Macros) (AutoCAD VBA)

Visual Basic provides numerous functions that you’ll find easy to use. This section introduces two of them—Date and Time—that may be useful to include in your DrawText macro. The Date function returns a Variant type containing the string representing the current date, and the Time function returns a Variant type containing a string representing the current time; the content and format of both strings are dependent on your PC’s settings.

Updating Your DrawText Macro

Because the Date and Time functions return strings, they can be used wherever any string is required in your code. Try replacing the "Hello" string in Line 7 of your DrawText macro with Date, as follows:


You don’t need to enclose Date in quotes because it is a function name. If you did use quotes, you would end up with the string "Date"in your drawing.

Run your macro again and watch as the current date appears in the Model Space tab of your drawing object, as shown here:


You can add both the time and date to your drawing by expanding your code statement as follows:


The ampersand character (&) concatenates strings, so that the last character of one string is joined up with the first character of the next string. The string containing one blank character in the middle adds a space in order to visually separate the two strings when displayed. Here’s how it looks:


Now that you have created a stand-alone macro, it’s time to create a macro for starting up the Metric-Imperial Converter application developed.This macro can be called from the AutoCAD window. All you need to do is provide the code for showing the UserForm from the application. Before you can call the application, however, it has to be loaded. The next section shows how this is achieved.

Next post:

Previous post: