gpt4 book ai didi

vba - 我可以更改 Word 2007 中插入表格命令的行为吗?

转载 作者:行者123 更新时间:2023-12-04 20:11:12 25 4
gpt4 key购买 nike

在 Word 2007 中,“插入”功能区选项卡有一个“表格”组,其中包含一个下拉按钮(标记为“表格”)。

下拉列表包含各种菜单项,例如“插入表格...”、“绘制表格”等。但是,在下拉列表的顶部有一个 10x8 的网格,您可以使用它来快速选择您的使用鼠标新建表。 (第一个问题:这东西到底叫什么?!!)。

我想覆盖默认行为,以便当用户使用上面提到的小部件“绘制”表格时,我可以更改表格中使用的段落样式(并且可能进行一些其他清理)。

我知道如何覆盖“插入表...”命令,但我不知道如何覆盖“小部件”的行为。可以做到吗?

最佳答案

在你继续阅读之前(因为这会很长)我想说我没有解决这个问题。然而,在我尝试解决——然后解决——这个问题的过程中,我发现了很多东西,我在这里记录下来,希望它们能帮助其他人找到解决方案。

在尝试确定可用于插入表格的 10x8 网格控件时,我发现在名为“TableInsertGeneral”的 Word 命令中列出了一个宏。

TableInsertGeneral listed in Word Macros dialog box

根据 Suzanne S. Barnhill 的说法,如果此功能在早期版本的 Word (Insert Table Drop Down Missing) 中停止运行,此宏将恢复网格。该宏存在于 Word 2007/2010 中,无法从宏对话框的“运行”按钮执行。双击该命令会关闭对话框,但不会公开执行任何其他操作。我还尝试通过创建一个名为 TableInsertGeneral 的 VBA 子程序来拦截它的功能,但是当我访问网格时,我放入该子程序的代码似乎没有被执行。但是,根据我的研究,我相信 TableInsertGeneral 宏确实与显示 10x8 网格有一些联系。

我还尝试通过更改 Word 功能区中的表格库来解决此问题。因为我无法直接访问任何控制网格的代码,所以我尝试隐藏表格库,然后将其替换为排除网格功能的重建库(从而允许段落样式和其他更改在全局范围内工作)。

首先我下载了​​一些工具:

  • Microsoft Office 的自定义 UI 编辑器(用于编辑功能区:Office Custom UI Editor)
  • Office 2007 控件 ID(用于识别各种功能区部件的名称:Office 2007 Control IDs in Excel worksheet)
  • Office 2007 图标库(用于访问 Word 中的内部图标以放置在重建的功能区中:Word 2007 Icons Gallery)

  • 使用 Microsoft Office 的自定义 UI 编辑器(它允许编辑 Word 2007 文档或模板中的 customui.xml 文件,而无需创建文件夹结构或维护 xml 文件之间的关系)我打开了一个模板并将此代码保存在文件中:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
    <tabs>
    <tab idMso="TabInsert">
    <group idMso="GroupInsertTables" visible="false" />
    <group id="CustomGroupInsertTables" label="Tables" insertBeforeMso="GroupInsertIllustrations">
    <gallery id="CustomTableInsertGallery"
    label="Table"
    imageMso="TableInsertGallery"
    size="large"
    columns="1"
    rows="4"
    screentip="Table"
    supertip="Insert or draw a table into the document."
    keytip="T"
    onAction="RibbonControl.galleryOnAction" >
    <item id="GridMessage" label="Draw Table Via Grid Has Been Removed" imageMso="TablesGallery"
    supertip="Provides information on why this template has different Ribbon controls."/>
    <button idMso="TableInsertDialogWord" />
    <button idMso="ConvertTextToTable" />
    <button idMso="TableExcelSpreadsheetInsert" />
    </gallery>
    </group>
    </tab>
    </tabs>
    </ribbon>
    </customUI>

    这成功地隐藏了原始表格库并将其替换为原始表格库的一些功能。 10x8 网格消失了,但我无法恢复 Draw Table 切换按钮和 Quick Tables 库。据我所知,XML 架构不允许将其中任何一个(存在于 Word 2007 的开箱即用版本中)嵌入到现有库中。因为我不喜欢删除功能(即使对于我认为不会使用的部分解决方案),我添加了一个绑定(bind)到消息框的按钮作为重建库中的第一项:

    Rebuilt Tables Gallery

    用于连接新的“通过已删除网格绘制表格”按钮的代码放置在名为 RibbonControl 的模块中:
    Sub GalleryOnAction(Control As IRibbonControl, selectedID As String, selectedIndex As Integer)

    If Documents.Count = 0 Then
    MsgBox "This control is disabled when there is no active document."
    Exit Sub
    End If

    Select Case Control.id
    Case "CustomTableInsertGallery"
    Select Case selectedIndex
    Case 0
    MsgBox "Explain changes to Ribbon interface here."
    Case Else
    'Do Nothing
    End Select
    End Select
    End Sub

    我不希望任何人使用这个部分解决方案,但是,如果可以实现恢复两个缺失控件的方法,这可能是一个很好的解决方法。顺便说一句,我从 Greg Maxey 的网站上改编了大部分内容:

    Customizing the Ribbon

    如果你读到这里,谢谢!我希望你通过自己的尝试取得更大的成功。

    关于vba - 我可以更改 Word 2007 中插入表格命令的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6983707/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com