《excel_vba_编程教程(完整版)》

下载本书

添加书签

excel_vba_编程教程(完整版)- 第79节


按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
6: Row                                         6: Row  
7: Cell                                        7: Cell  
8: Column                                      8: Column  
9: Row                                         9: Row  
10: Ply                                        10: Ply  
11: XLM Cell                                   11: XLM Cell  
12: Document                                   12: Document  
13: Desktop                                    13: Desktop  
14: Nondefault Drag and Drop                   14: Nondefault Drag and Drop  
15: AutoFill                                   15: AutoFill  
16: Button                                     16: Button  
17: Dialog                                     17: Dialog  
18: Series                                     18: Series  
19: Plot Area                                  19: Plot Area  
20: Floor and Walls                            20: Floor and Walls  
21: Trendline                                  21: Trendline  
22: Chart                                      22: Chart  
23: Format Data Series                         23: Format Data Series  
24: Format Axis                                24: Format Axis  
25: Format Legend Entry                        25: Format Legend Entry  
26: Formula Bar                                26: Formula Bar  
27: PivotTable Context Menu                    27: PivotTable Context Menu  
28: Query                                      28: Query  
29: Query Layout                               29: Query Layout  
30: AutoCalculate                              30: AutoCalculate  
31: Object/Plot                                31: Object/Plot  
32: Title Bar (Charting)                       32: Title Bar (Charting)  
33: Layout                                     33: Layout  
34: Pivot Chart Popup                          34: Pivot Chart Popup  
35: Phonetic Information                       35: Phonetic Information  
36: Auto Sum                                   36: Auto Sum  
37: Paste Special Dropdown                     37: Paste Special Dropdown  
38: Find Format                                38: Find Format  
39: Replace Format                             39: Replace Format  
40: Shapes                                     40: Shapes  
41: Inactive Chart                             41: Inactive Chart  
42: Excel Control                              42: Excel Control  
43: Curve                                      43: Curve  
44: Curve Node                                 44: Curve Node  
45: Curve Segment                              45: Curve Segment  
46: Pictures Context Menu                      46: Pictures Context Menu  
47: OLE Object                                 47: OLE Object  
48: ActiveX Control                            48: ActiveX Control  
49: WordArt Context Menu                       49: WordArt Context Menu  
50: Rotate Mode                                50: Rotate Mode  
51: Connector                                  51: Connector  
52: Script Anchor Popup                        52: Script Anchor Popup  
                                           245 

… 页面 262…

 53: Canvas Popup                            53: Canvas Popup  
 54: Organization Chart Popup                54: Organization Chart Popup  
 55: Diagram                                 55: Diagram  
 56: Add mand                             56: Layout  
 57: Built…in Menus                          57: Select  
 58: System                                  58: List Range Popup  
 59: Layout                                  59: List Range Layout Popup  
 60: Select                                  60: XML Range Popup  
                                             61: List Range Layout Popup  
                                             62: Built…in Menus  
  
现在,你已经知道里Excel快捷菜单的准确名称了,你可以轻易地添加其它经常用到的命令到这些 
菜单中的任意菜单中去。尽管从工具栏上点击打印图标或者选择文件|打印来打印工作表都是很容 
易的事情,但是你可能还是想将该打印命令添加到快捷菜单中去,当你在工作表标签上单击右键时 
就会出现在该快捷菜单上。我们来看看如何添加该选项到Ply菜单上去。  
2。  输入如下所示地过程AddToPlyMenu:  
     Sub AddToPlyMenu()   
         With ApplicationmandBars(〃Ply〃)   
              。Reset   
              ntrols。Add(Type:=msoControlButton; Before:=2)。Caption = _   
              〃Print。。。〃   
              ntrols(〃Print。。。〃)。OnAction = 〃PrintSheet〃   
         End With   
     End Sub   
     上面所用地Reset方法避免当你多次运行该过程时,将同样的选项放置到该快捷菜单上。  
3。  运行过程AddToPlyMenu,然后返回到代码窗口,并且输入下述过程,当你从该快捷菜单上选择 
    Print选项时,就会执行该过程:  
     Sub PrintSheet()   
         Application。Dialogs(xlDialogPrint)。Show   
     End Sub   
4。  切换到Excel应用程序窗口,并且在任何工作表标签上单击右键,选择Print选项,你应该可以 
    看到当你使用其它内置工具打印时看到的相同的对话框。  
      

                                                         
图12…10 自定义选项可以添加到内置快捷菜单上(参见Print选项在过程AddToPlyMenu里被添加上 
了)  

                                          246 

… 页面 263…

12。创建快捷菜单  

1。  在当前VBA工程的代码窗口里输入过程Create_ShortMenu,如下所示:  
     Sub Create_ShortMenu()   
         Dim sm As Object   
         Set sm = ApplicationmandBars。Add(〃Information〃; msoBarPopup)   
         With sm  
              ntrols。Add(Type:=msoControlButton)。Caption = 〃Operating System〃   
              With ntrols(〃Operating System〃)   
                   。FaceId = 1954   
                   。OnAction = 〃OpSystem〃   
              End With   
              ntrols。Add(Type:=msoControlButton)。Caption = 〃Total Memory〃   
              With ntrols(〃Total Memory〃)  
                   。FaceId = 1977   
                   。OnAction = 〃TotalMemory〃   
              End With   
              ntrols。Add(Type:=msoControlButton)。Caption = 〃Used Memory〃   
              With ntrols(〃Used Memory〃)   
                   。FaceId = 2081   
                   。OnAction = 〃UsedMemory〃   
              End With   
              ntrols。Add(Type:=msoControlButton)。Caption = 〃Free Memory〃   
              With ntrols(〃Free Memory〃)   
                   。FaceId = 2153   
                   。OnAction = 〃FreeMemory〃   
              End With   
         End With   
     End Sub   
     上面的过程创建了一个名为Information的自定义快捷菜单,并给它添加了四个命令。注意, 
     每个命令都指定了一个图标。当你从该快捷菜单选择一命令,步骤2里面的相应过程就会被执 
     行。  
2。  输入下面为Create_ShortMenu过程调用的过程:  
     Sub FreeMemory( )   
         MsgBox Application。MemoryFree & 〃 bytes〃; ; 〃Free Memory〃   
     End Sub   
     Sub OpSystem( )   
         MsgBox Application。OperatingSystem; ; 〃Operating System〃   
     End Sub   
     Sub TotalMemory( )   
         MsgBox Application。MemoryTotal; ; 〃Total Memory〃   
     End Sub   
     Sub UsedMemory( )   
         MsgBox Application。MemoryUsed; ; 〃Used Memory〃   
     End Sub   
     要将名为Information的自定义快捷键显示在屏幕上的话,你可以使用方法ShowPopup,如步 
骤3所示。  
3。  在立即窗口里输入下述语句:  
    mandBars(〃Information〃)。ShowPopup 0; 0   
    对象mandBar的方法ShowPopup接受两个可选参数(x; y),决定快捷菜单在屏幕上的位置。 
    在上面的例子里,快捷菜单Information将出现在屏幕的左上角。  

                                         247 

… 页面 264…

假设你正在设计一个自定义窗体,并想要当用户右键单击一个命令按钮时显示一个快捷菜单:  
1。  从VB编辑器菜单上,选择插入…用户窗体  
2。  使用工具箱上的命令控件,在空白用户窗体的任意位置放置一个按钮  
3。  通过点击工程浏览器窗口的查看代码按钮,切换到该窗体的代码窗口  
4。  在UserForm1代码窗口里输入下述过程:  
     Private Sub mandButton1_MouseDown(ByVal Button _   
       As Integer; _   
       ByVal Shift As Integer; _   
       ByVal X As Single; _   
       ByVal Y As Single)   
         If Button = 2 Then   
         Call Show_ShortMenu   
         Else   
         MsgBox 〃You must right…click this button。〃   
         End If   
     End Sub   
当用户右键点击窗体上按钮时,该过程就会调用过程Show_ShortMenu。点击鼠标按钮时VB会有两个 
小提示:按 回车 [Enter] 键 返回书目,按 ← 键 返回上一页, 按 → 键 进入下一页。 赞一下 添加书签加入书架