AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Home. Page II. Page III.

VBA and Menu's - Page I

Before starting on this tutorial, I presume that you are familiar with AutoCAD menu's and that you have a bit of experience in modifying menu's or creating your own partial menu's. It will also make things a lot easier for you if you have a basic understanding of AutoCAD's macro language.
If not, and you don't understand a word that I am talking about, I suggest you pop along to this tutorial, Creating Menu's, which should explain the basics to you.

Right, pull up a chair, grab a beer, and let's get started.


In VBA you can only create Pulldown and Toolbar menu's, although you can access Image, Screen and Tablet menu's.

This tutorial will demonstrate how to create Pulldown and Toolbar menu's, as well as show you a few examples on how to use menu macro's to call AutoCAD command functions, AutoLisp routines and Image menu's.

Let's begin with Pulldown menu's. First you need to create an empty file in Notepad called "VbaMenu.mns". Make sure that this file is within your AutoCAD search path. Fire up AutoCAD, open the VBA Editor and add this coding to a module :

'CODING STARTS HERE

Option Explicit

Sub VbaMenu1()

Dim currMenuGroup As AcadMenuGroup
Dim openMacro As String
Dim newMenuItem As AcadPopupMenuItem
Dim newScmenu As AcadPopupMenu
Dim newMenu As AcadPopupMenu

On Error GoTo Err_Control

'Load the VBAMENU menu
ThisDrawing.Application.MenuGroups.Load "VbaMenu.mns"

'We now need to obtain a reference to our new menu, VBAMENU
'Use MenuGroups property to obtain reference to main VBAMENU menu

Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item("VBAMENU")

'==========================================================
'Create Pulldown Menu
'==========================================================

'Creat the pulldown menu title
Set newMenu = currMenuGroup.Menus.Add("V&BA Menu")
'==========================================================
'create the first pulldown item, vbaload
openMacro = Chr(3) & Chr(3) & Chr(95) & "vbaload" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "VBA &Load", openMacro)
newMenuItem.HelpString = "Load a VBA Application"
'==========================================================
'create the second pulldown item, vbaide

openMacro = Chr(3) & Chr(3) & Chr(95) & "vbaide" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "VBA &Editor", openMacro)
newMenuItem.HelpString = "Switch to the VBA Editor"
'==========================================================
'create the third pulldown item, vbarun

openMacro = Chr(3) & Chr(3) & Chr(95) & "vbarun" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "VBA &Macro", openMacro)
newMenuItem.HelpString = "Run a VBA Macro"
'==========================================================
'create the fourth pulldown item, vbaman

openMacro = Chr(3) & Chr(3) & Chr(95) & "vbaman" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "&VBA Manager", openMacro)
newMenuItem.HelpString = "Display the VBA Manager"
'==========================================================
'insert a separator after the fourth menu item

newMenu.AddSeparator (5)
'==========================================================
'create a simple menu macro

openMacro = Chr(3) & Chr(3) & Chr(95) & "zoom" & Chr(32) & "w" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "&Zoom", openMacro)
newMenuItem.HelpString = "Zoom Window"
'===================================================================
'create a menu item that loads and runs an Autolisp routine

openMacro = Chr(3) & Chr(3) & Chr(95) & "(if (not c:ddvpoint) (load ""ddvpoint"")" & Chr(32) & "ddvpoint" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "View &Point", openMacro)
newMenuItem.HelpString = "View Point"
'===================================================================
'create a menu item that calls an Image menu

openMacro = Chr(3) & Chr(3) & Chr(95) & "$I=image_3dobjects $I=*"
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "&3D Objects", openMacro)
newMenuItem.HelpString = "3D Objects"
'===================================================================
'create a menu item with a hyperlink

openMacro = Chr(3) & Chr(3) & Chr(95) & "browser" & Chr(32) & "www.afralisp.com" & Chr(32)
Set newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "&AfraLisp.com", openMacro)
newMenuItem.HelpString = "Visit AfraLisp now, or else."
'===================================================================
'===================================================================
'insert the pulldown menu into the menu bar, third from the end

newMenu.InsertInMenuBar (ThisDrawing.Application.MenuBar.Count - 2)
'===================================================================

're-compile the VBAMENU menu - VBAMENU.MNC

currMenuGroup.Save acMenuFileCompiled

'save it as a MNS file.
currMenuGroup.Save acMenuFileSource

Just_Here:
Exit Sub

Err_Control:
Select Case Err.Number
'The menu exists, just exit
Case -2147024809
Err.Clear
Resume Just_Here
Case Else
MsgBox Err.Description
Exit Sub
End Select
End Sub

'CODING ENDS HERE

Save your project as VbaMenu1, return to AutoCAD and run the macro. You should have a new Pulldown menu on the menu bar that looks like this :

Try out each menu item. It works hey! Now navigate your way to were you stored VbaMenu.mns and open it. You should see something like this :

//
// AutoCAD menu file - O:\e51d\e51d1\VbaMenu.mns
//

***MENUGROUP=VbaMenu

***POP2
ID_mnuVBA Menu [V&BA Menu]
ID_VBA Load [VBA &Load]^C^C_vbaload 
ID_VBA Editor [VBA &Editor]^C^C_vbaide 
ID_VBA Macro [VBA &Macro]^C^C_vbarun 
ID_VBA Manager [&VBA Manager]^C^C_vbaman 
[--]
ID_Zoom [&Zoom]^C^C_zoom w 
ID_View Point [View &Point]^C^C_(if (not c:ddvpoint) (load "ddvpoint") ddvpoint 
ID_3D Objects [&3D Objects]^C^C_$I=image_3dobjects $I=*
ID_AfraLisp.com [&AfraLisp.com]^C^C_browser www.afralisp.com 

***TOOLBARS

***HELPSTRINGS
ID_VIEW POINT [View Point]
ID_AFRALISP.COM [Visit AfraLisp now, or else.]
ID_VBA MANAGER [Display the VBA Manager]
ID_VBA LOAD [Load a VBA Application]
ID_ZOOM [Zoom Window]
ID_VBA MACRO [Run a VBA Macro]
ID_3D OBJECTS [3D Objects]
ID_VBA EDITOR [Switch to the VBA Editor]

//
// End of AutoCAD menu file - O:\e51d\e51d1\VbaMenu.mns
//

You should also find VbaMenu.mnc and VbaMenu.mnr in the same folder. Even though the VbaMenu.mns file was empty when we started, VBA has created all the coding necessary and compiled the other menu support files required for the menu to run.

On the next page, we'll have a look at creating Toolbar menu's.



Home. Page II. Page III.
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website