使用 VBA 在 PowerPoint 表格中选择多行/多列

问题描述

我正在寻找一种使用 VBA(两行或两列彼此相邻)在 PowerPoint 表格中选择多个行(或列)的方法。这是可以使用鼠标手动实现的。

例如在这里尝试选择所选表格的前两行,我尝试添加 Replace:=False 但这不起作用(编译错误)。

ActiveWindow.Selection.ShapeRange.Table.Rows(1).Select
ActiveWindow.Selection.ShapeRange.Table.Rows(2).Select Replace:=False

有关如何实现这一目标的任何线索?

感谢任何帮助。

解决方法

我终于发现选择行和选择列是 PowerPoint 中的本机内置功能 - 如果用户选择了多个单元格,它们允许选择多个行或列。

根据 Microsoft 的文档,这些命令的名称为 TableRowSelectTableColumnSelect(Office 软件中内置功能区按钮的命令名称[请参见此处][1])。>

这些按钮可以通过以下方式在 VBA 中执行,这里以 SelectRow() 子为例。

Sub SelectRows()

'First we check some conditions to make sure that the user has selected a table,otherwise an error will occur if the user didn't select anything or if it selected something else than a table
If ActiveWindow.Selection.Type = 0 Then Exit Sub  'if nothing is selected the sub stops
If ActiveWindow.Selection.ShapeRange.Count > 1 Then Exit Sub 'if more than one shape is selected the sub stops
If ActiveWindow.Selection.ShapeRange(1).HasTable = False Then Exit Sub 'if this selected shape is not a table the sub stops
   
Application.CommandBars.ExecuteMso ("TableRowSelect")

End Sub

要将这个子链接到 SHIFT + SPACE 快捷方式,这可以在 AutoHotKey 中以 .ahk 文件中的以下方式实现:

#IfWinActive ahk_class PPTFrameClass
PPApp := ComObjActive("PowerPoint.Application")
    
; SHIFT + SPACE to select rows
+Space::

If PPApp.ActiveWindow.Selection.Type = 0 { 
Return 
}
If PPApp.ActiveWindow.Selection.Shaperange.Count <> 1 { 
Return 
}
If PPApp.ActiveWindow.Selection.Shaperange.HasTable = False {
 Return 
 }

PPApp.CommandBars.ExecuteMso("TableRowSelect")

Return

同样,CTRL + SPACE 可以与选择列命令相关联 - 请注意,这将覆盖 PowerPoint 的 CTRL + SPACE 内置快捷方式,该快捷方式会删除选定文本的格式。 [1]:https://github.com/OfficeDev/office-fluent-ui-command-identifiers