简体   繁体   English

“运行时错误'1004'无法在隐藏的工作簿上编辑宏。 在“功能区XML”中使用“ onLoad”时,使用“取消隐藏”命令取消隐藏工作簿

[英]“Run-time error '1004' Cannot edit a macro on a hidden workbook. Unhide the workbook using the Unhide command” When using “onLoad” in Ribbon XML

I have a feeling I'm using the "onLoad" callback incorrectly. 我有一种错误的使用“ onLoad”回调的感觉。 Essentially what I'm trying to do is run some code in VBA immediately after my custom ribbon loads (which is located on an add-in I've created). 本质上,我想做的是在自定义功能区加载后(位于我创建的加载项中)立即在VBA中运行一些代码。

This is the XML code: 这是XML代码:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="spRibbon_onLoad">

Here is the VBA code: 这是VBA代码:

This is a global declaration: 这是一个全局声明:

Dim grxIRibbonUI

This is the code I'm attempting to run: 这是我尝试运行的代码:

Sub spRibbon_onLoad(ribbon As IRibbonUI)
    Set grxIRibbonUI = ribbon
    Call spCode 'this is the code I'm attempting to run
End Sub

The problem is, if I try and open an existing workbook (when no other instances of excel are open) I get the following error: 问题是,如果我尝试打开一个现有的工作簿(没有打开其他任何excel实例时),则会收到以下错误:

Run-time error '1004': Cannot edit a macro on a hidden workbook. 运行时错误'1004':无法在隐藏的工作簿上编辑宏。 Unhide the workbook using the Unhide command. 使用取消隐藏命令取消隐藏工作簿。

I then have to select the "End" button twice on the error because it pops up twice. 然后,我必须在错误上选择两次“结束”按钮,因为它会弹出两次。

I guess I can understand why it's throwing that error, my custom add-in hasn't loaded the customized ribbon yet but since the main ribbon has loaded it's attempting to run a macro it can't see. 我想我能理解为什么会引发该错误,我的自定义加载项尚未加载自定义功能区,但是由于主功能区已加载,因此它试图运行一个宏,因此看不到。 However, if I have a workbook open already with my custom ribbon already visible, it simply won't run the code. 但是,如果我已经打开了一个工作簿,并且我的自定义功能区已经可见,那么它将根本不会运行代码。 Any suggestions would be appreciated. 任何建议,将不胜感激。

After a little more research I found out that a simple time delay of one second accomplished this. 经过更多的研究,我发现只需一秒钟的时间延迟就可以完成此操作。 It's not exactly what I was looking for but it works so I've decided to answer my own question. 这不是我一直在寻找的东西,但是它可以工作,所以我决定回答自己的问题。

Sub spRibbon_onLoad(ribbon As IRibbonUI)
    Application.OnTime Now + TimeValue("00:00:01"), "spCode"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM