Tải bản đầy đủ - 0 (trang)
Chapter 19. Adding Menus and Toolbars

Chapter 19. Adding Menus and Toolbars

Tải bản đầy đủ - 0trang

19.1.AboutExcelMenus

Excelhastwotop-levelmenubars,andwhichmenubaris

displayeddependsonwhathasfocusinExcel.Theworksheet

menubar(Figure19-1)isdisplayedwhenExcelfirststartsup,

whenaworksheethasfocus,andwhenallworkbooksare

closed.



Figure19-1.Worksheetmenubar



Thechartmenubar(Figure19-2)appearswhenachartsheet

oranembeddedchartobjecthasfocus.



Figure19-2.Chartmenubar



Therearealsocontextmenusforjustabouteveryiteminthe

Excelinterface.Contextmenuspopupwhenyouright-clickan

iteminExcel.Forexample,Figure19-3showsthecontext

menudisplayedwhenyouright-clickarangeofcells.



Contextmenusarealsosometimescalledshortcutmenus.



Figure19-3.Thecellcontextmenu



Youcanchangeanyofthesetypesofmenusto:

Addordeleteexistingcommands

Createnewitemsthatruncode

Buildcustommenusofitemswithmultiplelevels

Thefollowingsectionsdetailhowtocreateandmodifyeachof

thesetypesofmenus.







19.2.BuildaTop-LevelMenu

Top-levelmenusappearoneithertheworksheetmenubaror

thechartmenubar(Figure19-1orFigure19-2).Toaddatoplevelmenutotheworksheetmenubar,selectaworksheet

beforecreatingthemenu.Toaddatop-levelmenutothechart

menubar,selectachartbeforecreatingthemenu.Toaddthe

menutobothmenubars,createthemenutwiceonceforeach

menubar.

Tocreateanewtop-levelmenuonamenubarinExcel:

1. ChooseTools Customize Commands.Exceldisplays

theCommandstaboftheCustomizedialogbox.

2. IntheCategorieslist,chooseNewMenu.ClickNewMenuin

theCommandslistanddragitontotheExcelmenubaras

showninFigure19-4.Exceladdsanewtop-levelmenuto

themenubar.



Figure19-4.Steps1and2:addthetop-level

menu



3. Right-clickonthenewmenuitemandrenameitasshown

inFigure19-5.

4. IntheCategorieslist,chooseMacros,thenclickCustom

MenuItemfromtheCommandslistanddragitontothe

newmenuitemasshowninFigure19-6.

5. Right-clickthenewmenuitem,renameit,andassignitto

runamacroasshowninFigure19-7.

6. ClickCloseontheCustomizedialogboxwhenyouaredone

addingitemsandsettingmenuproperties.



Right-clickingdisplaysthemenupropertiesshowninFigure19-5and

Figure19-7onlywhiletheCustomizedialogisopen.



Figure19-5.Step3:renamethemenu



Figure19-6.Step4:additemstothemenu



Figure19-7.Step5:renamemenuitemand

assignamacro



19.2.1.ChangeExistingMenus

Youcanalsoaddbuilt-incommandsorcreatenewcustom

commandsonexistingExcelmenus.Toaddbuilt-incommand

toanexistingmenu:

1. ChooseTools Customize Commands.Exceldisplays

theCommandstaboftheCustomizedialogbox.

2. IntheCategorieslist,choosethecategoryoftheexisting

commandyouwanttoadd.Eachcategorycontainsalong

listofcommands,soyoumayneedtocheckacouple



differentcategoriesbeforeyoufindthecommandyouwant.

3. DragtheexistingcommandfromtheCommandslistonto

thetop-levelmenuyouwanttoadditto.Exceldisplaysthe

menuonceyoudragoveritandyoucandragthecommand

downtothepositionwhereyouwantittoappearonthe

menuasshowninFigure19-8.

4. ClickCloseontheCustomizedialogboxwhenyouaredone

addingitemsandsettingmenuproperties.

Toaddanewcustomcommandtoanexistingmenu,repeatthe

precedingprocedure,butselectMacrosfromtheCategorieslist

andCustomMenuItemfromtheCommandslistinStep2.You

canthenrenameandassignamacrotothenewmenuitemas

showninFigure19-7.



Figure19-8.Dragginganexistingcommandonto

amenu



19.2.2.AssignAcceleratorandShortcutKeys

Itisstandardpracticetoprovidekeyboardalternativestousing

point-and-clickmenus.Therearetwowaystodoso:



Acceleratorkeys

Appearunderlinedinthemenuname.Theyallowyouto

activatethemenuitembypressingAlt-key.Forexample

Alt-F-SsavesanExcelworkbook.



Shortcutkeys

Appearnexttothemenuitem.Theyprovidedirectaccess

toatasklistedonamenu.Forexample,Ctrl-Ssavesan

ExcelworkbookthesameasAlt-F-S.

Toassignanacceleratorkeytoamenuitem,useanampersand

(&)inthemenunamebeforetheacceleratorkey.Forexample,

&RundefinesRastheacceleratorkeyforthemenu.

Toassignashort-cutkeytoamenuitem:

1. ChooseTools Customize Commands.Exceldisplays

theCommandstaboftheCustomizedialogbox.

2. Right-clickonthenewmenuitemandrenametoaddthe

short-cutsequenceafterthemenuitem'sname.For

example,Run&AllCtrl-Shift-R.

3. ClosetheCustomizedialogbox.

4. ChooseTools

dialogbox.



Macro



MacrostodisplaytheMacro



5. Selectthemacrothatisassignedtothemenuitemand

clickOptions.

6. Pressthelettertoassignastheshort-cut.PressingShift

addsthatkeytothecombinationasshowninFigure19-9.

7. TypeadescriptionandpressOKtwicetoclosethedialogs.



Figure19-9.Assigningashortcutkeysequence



Menusoftengroupsimilaritemsusingseparatorbars.Tostarta

newgroupofitemsbyaddingaseparatorbar:

1. ChooseTools Customize Commands.Exceldisplays

theCommandstaboftheCustomizedialogbox.

2. Right-clickonthemenuitembeforewhichtoaddthe

separatorbarandselectBeginaGroup.



3. ClosetheCustomizedialogbox.Exceladdsaseparatorbar

beforetheitemasshowninFigure19-10.



Figure19-10.Menuitemswithacceleratorand

shortcutkeysandaseparatorbar



19.2.3.SaveandDistributeMenus

WhenyoucloseExcel,anymenuchangesyoumadeare

automaticallysavedinan.xlbfile.Thefilenameandlocation

variesbasedontheversionofExcel.Versions2000,XP,and

2003areExcel9.xlb,Excel10.xlb,andExcel11.xlb,respectively.

OnWindowsXP,thefilesarestoredinthe

%UserProfile%\ApplicationData\Microsoft\Excel\folder.

Asaresult,custommenusareuser-specific.Ifyouwantto

distributeacustommenutoothers,youmusteitherreplace

their.xlbfilewithyourownoryoumustdynamicallycreatethe

menuincode.Creatingthemenuincodeisthebestoptionin

mostcasesbecausethatapproach:

Doesn'toverwritetheusers'ownmenuchanges,theway

thatreplacingtheir.xlbfiledoes

Allowsyoutoassociatethemenuswiththefilecontaining

thecodesothatthemenusappearonlyifthatworkbook,



template,oradd-inisloaded

Thefollowingsectiondescribeshowtocreateatop-levelmenu

incodesothatyoucandistributeitaspartofaworkbook,

template,oradd-in.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 19. Adding Menus and Toolbars

Tải bản đầy đủ ngay(0 tr)

×