本文介绍了在 PowerPoint 启动时加载功能区控件时定期获取 VBA 中断模式错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我为 PowerPoint 创建了一个加载项,它根据您选择的对象创建一个对象网格.加载项在功能区中使用上下文相关控件,因此该控件仅在选择一个或多个形状时可用(否则呈灰色).当我打开 PowerPoint(启用此加载项)时,我可能会收到以下错误消息:PowerPoint 无法启动此功能,因为您当前有一个处于中断模式的 Visual Basic for Applications 项目."当每个自定义控件加载到功能区时,该消息会重复出现(当您对每个错误单击确定"时,控件会逐渐出现),一旦 PowerPoint 启动,这些控件就会出现在功能区中,但呈灰色显示,并且无法获取选择形状时激活.错误很难找到:它可能会消失,不会在特定机器上重新出现,但可能会出现在不同机器上的相同设置(和完全相同的加载项代码).如果此加载项单独加载,则可能会发生这种情况,或者仅当它与其他加载项一起加载时才会发生.当错误消息出现时,我可能会通过注释掉部分代码来查找错误,将其保存并重新添加为 ppam 加载项,直到错误消息消失:然后尝试验证我是否找到了问题的根源,取消注释掉我注释掉的代码,直到错误再次出现 - 除了通常错误不会再次出现.如果 PowerPoint 以打开的文档开始,则不会出现该错误.此外,只有当 PowerPoint 设置为使用欢迎屏幕而不是新文档启动时,才会出现该错误.我目前的办公版本是:1812(office 365 - 桌面版),build 11126.20196(但我在其他版本上看到过这个)和 Windows 10 版本 1803.我试过了:将我的所有代码导出到文本文件,然后使用适用于 PowerPoint 的 Rob Bovey 代码清理器版本将其导入:http://www.tushar-mehta.com/powerpoint/vba_codecleaner/index.htm试图强制显示错误消息以尝试找出错误的可能来源 - 但到目前为止还无法做到这一点:在 PowerPoint 加载时故意在任何运行的代码中插入错误(例如除以零)会导致通常的调试错误消息,而不是我遇到问题的错误消息.在 Auto_Open、onLoad 和 getEnabled 调用中引入延迟,以尝试获取可能异步运行的任何这些调用以在 建议 Auto_Open 和 onLoad 调用可能相互冲突:但是虽然我可以让 Auto_Open 和 onLoad 调用重叠,这并没有导致错误信息再次出现.查看问题是否可能由于 PowerPoint 上次关闭时的问题而发生:我将 PowerPoint 置于中断模式,然后使用任务管理器强制它停止,然后重新启动 PowerPoint - 但没有看到任何错误.我还尝试在中断模式 (Excel) 下使用另一个办公室 VBA 项目,然后启动 PowerPoint - 再次没有看到错误.我已经尝试比较 PowerPoint 处于中断模式时的注册表 (HKCU) 以及何时未查看是否可以提供线索 - 但没有什么不同的重要.GridMaker 加载项的 XML 如下所示(在 customUI.xml 中 - 我在 Visual Studio 中使用 Open XML Package Editor 扩展打开了 pptm 文件):<customUI onLoad="GMApplicationEventsModule.gmRibbonUI_onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:nsCommtap="CommtapNamespace"><ribbon startFromScratch="false"><tab idMso="TabHome"><group id="gridMakerGroup" label="Grid Maker"><button id="gmMakeGrid"标签=制作成网格"imageMso="表格插入"大小=正常"onAction="MainModule.makeGrid"getEnabled="GMApplicationEventsModule.gm_GetEnabled"/></组></tab></tabs></色带></customUI>GMApplicationEventsModule 如下所示:选项显式私有 m_oMyRibbon 作为 IRibbonUI私有 m_boolShapeSelected 作为布尔值私有 m_oAppEvents 作为 CGMApplicationEvents公共函数 get_m_oMyRibbon() 作为 IRibbonUI设置 get_m_oMyRibbon = m_oMyRibbon结束函数Public Sub set_m_boolShapeSelected(value As Boolean)m_boolShapeSelected = 值结束子公共函数 get_m_boolShapeSelected() 作为布尔值get_m_boolShapeSelected = m_boolShapeSelected结束函数Public Sub gm_GetEnabled(control As IRibbonControl, ByRef returnedVal)返回值 = 真如果 control.Id = "gmMakeGrid" 然后返回值 = GMApplicationEventsModule.get_m_boolShapeSelected万一结束子公共子 gmRibbonUI_onLoad(Ribbon As IRibbonUI)设置 m_oMyRibbon = 功能区结束子公共子 Auto_Open()如果 m_oAppEvents 什么都没有,那么设置 m_oAppEvents = 新 CGMApplicationEvents万一设置 m_oAppEvents.App = 应用程序结束子CGMApplicationEvents 类:选项显式Public WithEvents 应用程序作为应用程序Private Sub App_WindowSelectionChange(ByVal Sel As Selection)如果 Application.ActiveWindow.Selection.Type ppSelectionShapes 然后GMApplicationEventsModule.set_m_boolShapeSelected False别的GMApplicationEventsModule.set_m_boolShapeSelected 真万一如果不是 GMApplicationEventsModule.get_m_oMyRibbon 那么什么都不是Dim oRibbon 作为 IRibbonUI设置 oRibbon = GMApplicationEventsModule.get_m_oMyRibbonoRibbon.InvalidateControl "gmMakeGrid"万一结束子我还有另外两个看起来像这样的插件.gmMakeGrid 按钮(上面)上的 onAction 运行一些代码,这些代码从形状选择中创建一个形状网格.其他两个加载项仅在单击其按钮时显示一条消息.但是,如果此处显示的代码仅链接到显示消息框,则应该有可能(或不可能 - 因为错误似乎是间歇性的)看到错误.显示错误信息我现在可以显示错误消息(PowerPoint 无法启动此功能..."),这也表明错误的来源可能来自加载项以外的其他地方出现错误被看见了.我有一个已启用的加载项 - app1-basic(使用上述代码).我有第二个宏 (app2-basic.pptm) 文件(也如上所述),其中我在 onLoad 过程中放置了一个停止"语句.关闭 PowerPoint,打开 app2-basic.pptm.打开时,VBE 将打开并且代码执行将在 stop 语句处停止.现在关闭 PowerPoint(通过单击部分打开的 PowerPoint 中的X").这将显示错误消息.错误消息似乎被触发,因为 PowerPoint 正在尝试调用加载项中的 getEnabled 过程,但无法调用,因为正在打开的宏已将其置于中断模式.日志记录似乎支持这一点:Time Time tick VBA 项目模块程序注释在 app2-basic onLoad 中停止,它以 pptm(不是加载项)的形式打开,启用了另一个加载项(app1-basic)25/02/19 15:17:58:258 158,650,603,696 app1-basic ApplicationEventsModule Auto_Open 被调用25/02/19 15:17:58:258 158,650,619,479 app1-basic ApplicationEventsModule Auto_Open 已完成25/02/19 15:17:59:259 158,653,089,479 app1-basic ApplicationEventsModule set_m_boolShapeSelected 被调用25/02/19 15:17:59:259 158,653,105,936 app1-basic ApplicationEventsModule get_m_oMyRibbon 被调用25/02/19 15:17:59:259 158,653,421,625 app1-basic ApplicationEventsModule gmRibbonUI_onLoad 被调用.25/02/19 15:17:59:259 158,653,477,892 app1-basic ApplicationEventsModule gmRibbonUI_onLoad 已完成25/02/19 15:17:59:259 158,653,560,560 app2-basic ApplicationEventsModule gmRibbonUI_onLoad 被调用.正常打开然后关闭 - app1 - 没有错误25/02/19 15:34:45:245 161,505,047,523 app1-basic ApplicationEventsModule Auto_Open 调用25/02/19 15:34:45:245 161,505,061,039 app1-basic ApplicationEventsModule Auto_Open 已完成25/02/19 15:34:54:254 161,531,088,269 app1-basic ApplicationEventsModule gmRibbonUI_onLoad 被调用.25/02/19 15:34:54:254 161,531,110,752 app1-basic ApplicationEventsModule gmRibbonUI_onLoad 已完成25/02/19 15:34:54:254 161,531,169,707 app1-basic ApplicationEventsModule app1_GetEnabled 调用:Control.id:app1ShapeSelectedMsg25/02/19 15:34:54:254 161,531,200,726 app1-basic ApplicationEventsModule get_m_boolShapeSelected Called25/02/19 15:34:54:254 161,531,215,706 app1-basic ApplicationEventsModule app1_GetEnabled 完成:Control.id:app1ShapeSelectedMsg进一步评论:有时,在多次出现错误并纠正它们之后,在加载没有错误的加载项时会出现此错误消息.我仍然无法想出导致这种情况发生的可重复操作序列.它是看起来错误很可能是由加载项以外的其他地方引起的,该加载项由于错误而无法正确加载.错误似乎也可能是由于 PowerPoint 的打开状态出现问题 - 可能是由于上次关闭时遇到的问题.代码演示错误产生此错误信息的完整代码不时. 解决方案 不幸的是我一直无法解决这个问题,所以这个问题总是会出现 - 无论我有什么设置,问题似乎都是随机出现的,然后随机消失.我的解决方案是完全回避这个问题,不使用 Auto_Open(如果不使用 Auto_Open,这个问题永远不会发生),也不使用 _onLoad 这也可能有问题.这意味着按钮将始终可用,即使在它们不起作用的情况下也是如此.为了使这种情况对用户更加友好(而不只是弹出一些恼人的消息框错误来告诉人们他们点击了无效按钮),我已经安排了它,以便如果用户选择一个按钮不适用于当前上下文 - 例如,该操作要求他们选择一些文本才能使其工作,但他们没有 - 将弹出一个无模式对话框,其中包含他们需要做什么才能使按钮生效的说明工作(例如选择一些文本)加上功能区组中按钮的精确副本,其中按钮根据上下文正确启用/禁用自身.我通过在每个按钮的三种状态(悬停、启用、禁用)中裁剪每个按钮的屏幕截图并将它们在用户表单中相互叠加来创建这些按钮.我使用app_windowselectionchange事件更改相关按钮的zorder,以便选择相应的东西,启用的按钮到顶部,并且鉴于选择正确的东西,按钮的悬停版本将到顶部当鼠标悬停在它上面时(用户表单中的 MouseMove 触发器) - 这也是按钮的版本,单击时将触发任何代码.我认为这不是一个糟糕的解决方案,因为用户可以获得更多关于何时可以使用按钮的信息,并且他们可以有一个对话框,如果他们愿意,他们可以以与功能区相同的方式工作.我们的按钮组很小(不超过四个按钮),所以对话框也很小,很容易显示和推开 - 对于其中有很多按钮的组来说可能不太令人满意.I have created an add-in for PowerPoint which creates a grid of objects from a selection of objects that you select.The add-in uses a context-sensitive control in the ribbon so that the control is only available when one or more shapes are selected (otherwise it is greyed out).When I open PowerPoint (with this add-in enabled), I may get the following error message: "PowerPoint can't start this feature because you currently have a Visual Basic for Applications project in break mode."The message repeats as each custom control is loaded onto the ribbon (the controls progressively appear as you click ok to each error), and once PowerPoint has started the controls are present in the ribbon but are greyed out, and do not get activated when a shape is selected.The error is very hard to fault find: it may just disappear and not re-appear on a particular machine, but then may be present on a different machine for the same set-up (and exactly the same add-in code). It may occur if this add-in is loaded on its own, or it might only occur if it is loaded with other add-ins.When the error messages appear, I may fault-find by commenting out sections of code, save and re-add it as a ppam add-in until the error messages disappear: and then to try and verify that I'd found the source of the problem, uncomment out the code I'd commented out until the error re-appears - except that usually the error does not re-appear.The error is not seen if PowerPoint starts with an open document. Also the error might only be seen when PowerPoint is set up to start up with its welcome screen rather than a new document.My current office version is: 1812 (office 365 - desktop), build 11126.20196 (but I have seen this on other versions) and Windows 10 version 1803.I have tried:Exporting all my code to text files and importing it back using a version of Rob Bovey's code cleaner adapted for PowerPoint: http://www.tushar-mehta.com/powerpoint/vba_codecleaner/index.htmTrying to force the error messages to appear to try and work out what a possible source of the errors could be - but have so far been unable to do this:Deliberately inserting an error in any of the code run when PowerPoint loads (such as divide by zero) results in the usual debug error messages and not the error messages that I am having a problem with.Introducing delays in Auto_Open, onLoad and getEnabled calls to try and get any of these calls that might run asynchronously to overlap following a suggestion that Auto_Open and onLoad calls may be fighting with each other: but although I can get Auto_Open and onLoad calls to overlap, this hasn't caused the re-appearance of the error messages.Seeing if the problem might happen due to a problem when PowerPoint was last closed: I put PowerPoint in break mode, then forced it to stop with the task manager and then re-started PowerPoint - but no errors seen. Also I have tried having another office VBA project in break mode (Excel) and then start PowerPoint - again no errors seen.I've tried comparing the registry (HKCU) for when PowerPoint is in break mode and when it isn't to see if that might give a clue - but there was nothing significant that was different.The XML for the GridMaker add-in looks like this (in customUI.xml - I open the pptm file in Visual Studio with the Open XML Package Editor extension):<customUI onLoad="GMApplicationEventsModule.gmRibbonUI_onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:nsCommtap="CommtapNamespace"> <ribbon startFromScratch="false"> <tabs> <tab idMso="TabHome"> <group id="gridMakerGroup" label="Grid Maker"> <button id="gmMakeGrid" label="Make Into Grid" imageMso="TableInsert" size="normal" onAction="MainModule.makeGrid" getEnabled="GMApplicationEventsModule.gm_GetEnabled" /> </group> </tab> </tabs> </ribbon></customUI>The GMApplicationEventsModule looks like this:Option ExplicitPrivate m_oMyRibbon As IRibbonUIPrivate m_boolShapeSelected As BooleanPrivate m_oAppEvents As CGMApplicationEventsPublic Function get_m_oMyRibbon() As IRibbonUI Set get_m_oMyRibbon = m_oMyRibbonEnd FunctionPublic Sub set_m_boolShapeSelected(value As Boolean) m_boolShapeSelected = valueEnd SubPublic Function get_m_boolShapeSelected() As Boolean get_m_boolShapeSelected = m_boolShapeSelectedEnd FunctionPublic Sub gm_GetEnabled(control As IRibbonControl, ByRef returnedVal) returnedVal = True If control.Id = "gmMakeGrid" Then returnedVal = GMApplicationEventsModule.get_m_boolShapeSelected End IfEnd SubPublic Sub gmRibbonUI_onLoad(Ribbon As IRibbonUI) Set m_oMyRibbon = RibbonEnd SubPublic Sub Auto_Open() If m_oAppEvents Is Nothing Then Set m_oAppEvents = New CGMApplicationEvents End If Set m_oAppEvents.App = ApplicationEnd SubClass CGMApplicationEvents:Option ExplicitPublic WithEvents App As ApplicationPrivate Sub App_WindowSelectionChange(ByVal Sel As Selection) If Application.ActiveWindow.Selection.Type <> ppSelectionShapes Then GMApplicationEventsModule.set_m_boolShapeSelected False Else GMApplicationEventsModule.set_m_boolShapeSelected True End If If Not GMApplicationEventsModule.get_m_oMyRibbon Is Nothing Then Dim oRibbon As IRibbonUI Set oRibbon = GMApplicationEventsModule.get_m_oMyRibbon oRibbon.InvalidateControl "gmMakeGrid" End IfEnd SubI have another two add-ins which look like this. The onAction on the gmMakeGrid button (above) runs some code which creates a grid of shapes from a shape selection. The other two add-ins simply show a message when their buttons are clicked. But it should be possible (or not - as the errors seem to be intermittent) to see the errors if the code shown here is just linked to showing a message box.Making the error message appearI am now able to make the error message ("PowerPoint can't start this feature...") appear, which also shows that the source of the error could originate from somewhere other than the add-in where the error is seen.I have one add-in - app1-basic (using the code as described above) which is enabled. I have a second macro (app2-basic.pptm) file (also as described above) in which I have put a "Stop" statement in the onLoad procedure. With PowerPoint closed, open app2-basic.pptm. Whilst opening, the VBE will open and code execution will stop at the stop statement. Now close PowerPoint (by clicking on the "X" in the partially opened PowerPoint). This will display the error message.The error message appears to be triggered because PowerPoint is trying to call the getEnabled procedure in the add-in but is unable to because the macro being opened has put it in break mode. Logging seems to support this:Time Time tick VBA Project Module Procedure CommentStop in app2-basic onLoad which is opened as pptm (not add-in), one other add-in enabled (app1-basic)25/02/19 15:17:58:258 158,650,603,696 app1-basic ApplicationEventsModule Auto_Open Called25/02/19 15:17:58:258 158,650,619,479 app1-basic ApplicationEventsModule Auto_Open Completed25/02/19 15:17:59:259 158,653,089,479 app1-basic ApplicationEventsModule set_m_boolShapeSelected Called25/02/19 15:17:59:259 158,653,105,936 app1-basic ApplicationEventsModule get_m_oMyRibbon Called25/02/19 15:17:59:259 158,653,421,625 app1-basic ApplicationEventsModule gmRibbonUI_onLoad Called.25/02/19 15:17:59:259 158,653,477,892 app1-basic ApplicationEventsModule gmRibbonUI_onLoad Completed25/02/19 15:17:59:259 158,653,560,560 app2-basic ApplicationEventsModule gmRibbonUI_onLoad Called.Normal open then close - app1 - with no errors25/02/19 15:34:45:245 161,505,047,523 app1-basic ApplicationEventsModule Auto_Open Called25/02/19 15:34:45:245 161,505,061,039 app1-basic ApplicationEventsModule Auto_Open Completed25/02/19 15:34:54:254 161,531,088,269 app1-basic ApplicationEventsModule gmRibbonUI_onLoad Called.25/02/19 15:34:54:254 161,531,110,752 app1-basic ApplicationEventsModule gmRibbonUI_onLoad Completed25/02/19 15:34:54:254 161,531,169,707 app1-basic ApplicationEventsModule app1_GetEnabled Called: Control.id: app1ShapeSelectedMsg25/02/19 15:34:54:254 161,531,200,726 app1-basic ApplicationEventsModule get_m_boolShapeSelected Called25/02/19 15:34:54:254 161,531,215,706 app1-basic ApplicationEventsModule app1_GetEnabled Completed: Control.id: app1ShapeSelectedMsgFurther comments:From time to time, after a number of rounds of making errors appear and then correcting them, this error message can then appear when loading add-ins where there are no errors. I've still not been able to come up with a repeatable sequence of actions that causes this to happen.It is looking like the error is likely to be arising from somewhere else other than the add-in which is failing to load properly because of the error. It also seems possible that the error could be due to a problem with the state in which PowerPoint is opened - perhaps due to a problem encountered when it was last closed.Code to demonstrate the errorsFull code which produces this error message from time to time. 解决方案 Unfortunately I have been unable to make it so this problem will always appear - whatever set up I have, the problem seems to appear randomly and to disappear randomly.My solution is to sidestep the problem completely and not to use Auto_Open (this problem never occurs if Auto_Open is not used) and not to use _onLoad which can also be problematic. This means that the buttons will always be available even for contexts where they won't work.To make this situation a bit more user friendly (and not just have some irritating message box error popping up to tell people they've clicked on an invalid button), I have arranged it so that if a user selects a button which doesn't apply to the current context - for example the operation requires them to select some text for it to work but they haven't - a modeless dialog will pop up which contains an explanation of what they need to do to get the button to work (e.g. select some text) plus an exact replica of the buttons from the group in the ribbon in which the buttons enable/disable themselves correctly according to the context.I've created these buttons by cropping screenshots of each button in each of their three states (hover, enabled, disabled) and layering them on top of each other in a userform. I use the App_WindowSelectionChange event to change the ZOrder of the relevant buttons so that when the appropriate thing is selected the enabled button comes to the top, and, given that the right thing is selected, the hover version of the button will come to the top when the mouse is over it (MouseMove trigger in the userform) - this is also the version of the button which will trigger any code when clicked.I think this is not a bad solution because the user gets more information about when they can use the buttons and they can have a dialog present that works in the same way as the ribbon should have worked if they so wish. Our button groups are quite small (no more than four buttons) so the dialog is quite small too and easy to have displayed and pushed out of the way - it might not be so satisfactory for groups with lots of buttons in them. 这篇关于在 PowerPoint 启动时加载功能区控件时定期获取 VBA 中断模式错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!