简体   繁体   English

使用VBA Excel启用按钮

[英]Enable button using VBA excel

I have an uncommon issue. 我有一个不常见的问题。 I have an Excel file with macros. 我有一个带有宏的Excel文件。 On all computers it was installed Office 2010 and everything worked fine, but a few days ago every computer was upgraded to Office 2013. In code, at some point I enable or disable some buttons based on some criterias. 在所有计算机上都安装了Office 2010,并且一切正常,但是几天前,每台计算机都已升级到Office2013。在代码中,有时我会根据某些条件启用或禁用某些按钮。

 ThisWorkbook.Worksheets("Lab Orders").OLEObjects("CommandButton1").Enabled = False

On some computers this works fine , but on other computers this button is shown as enabled, and if the user clicks the button nothing happens, not even the animation where the the button is pressed, so it cannot execute the code behind. 在某些计算机上,此方法工作正常,但在其他计算机上,此按钮显示为启用状态,并且如果用户单击该按钮,则什么也不会发生,即使按下该按钮的动画也不会发生,因此它无法执行后面的代码。 It's like an image. 就像一张图片。 Any leads, or reasons why this is happening only on certain computers ? 有什么线索,或者是为什么仅在某些计算机上发生这种情况的原因? I have this problem after the Office got upgraded. Office升级后出现此问题。 PS. PS。 All computers are in the same domain. 所有计算机都在同一域中。

LATER EDIT 后期编辑

I changed the code line 我更改了代码行

ThisWorkbook.Worksheets("Lab Orders").CommandButton1.Enabled = True

and now I get this error: 438 at this row (I debbuged the file on a computer where this file does not work) 现在我收到此错误:438在此行(我在该文件无法工作的计算机上对该文件进行了调试)

我怀疑这是Office最新更新的问题-请查看是否可以解决此问题: http : //excelmatters.com/2014/12/10/office-update-breaks-activex-controls/

On all computers where you have the problem, check the box " Trust access to the VBA project object model " located in Options/Trust Center/Macro Settings 在出现问题的所有计算机上,选中选项/信任中心/宏设置中的“ 信任对VBA项目对象模型的访问 ”框

(Note that this will only apply to Excel, and if you are having the same problems on other MS Office softwares, you should do the same on them as well) (请注意,这仅适用于Excel,并且如果您在其他MS Office软件上遇到相同的问题,则也应对它们执行相同的操作)

If that's still not working, read and apply all the checks on this page : https://support.office.com/en-gb/article/Enable-or-disable-macros-in-Office-files-12b036fd-d140-4e74-b45e-16fed1a7e5c6 如果仍然无法使用,请阅读并应用此页面上的所有检查: https : //support.office.com/en-gb/article/Enable-or-disable-macros-in-Office-files-12b036fd-d140- 4e74-b45e-16fed1a7e5c6

Did not fit as a comment, so: If I understand you correctly, some computers show the button enabled, while in fact it is disabled, right? 不适合作为评论,因此:如果我对您的理解正确,某些计算机会显示该按钮已启用,而实际上该按钮已被禁用,对吗?
In this case it's normal no button press animation "visibly happens" as Windows knows the button was programmatically disabled. 在这种情况下,由于Windows知道已以编程方式禁用了按钮,因此通常不会“明显发生”按钮按下动画。
The same thing happens if you hook a window (which has buttons on it) and send any button a WM to show itself as if it was enabled (while in fact it is not). 如果您钩住一个窗口窗口上有按钮)并向任何按钮发送WM以显示其自身,就好像发生了激活一样(实际上没有启用),则会发生相同的事情。

The reason it seems enabled on some computers may vary, most probably it's video card and/or driver 'issue', or some "strange alignment" (that particular OS with that particular graphics card & driver and DirectX, as well as those 'special' OLE settings, etc). 似乎在某些计算机上启用它的原因可能有所不同,很可能是视频卡和/或驱动程序的“问题”,或某些“奇怪的对齐方式”(具有该特定图形卡和驱动程序以及DirectX的特定OS,以及那些“特殊” 'OLE设置等)。
In both cases, the screen area don't get "refreshed" ( invalidated ) after the button was disabled - so it seems enabled and gets "repainted" only when there was some user interaction around it. 在这两种情况下,禁用按钮后,屏幕区域都不会“刷新”( 无效 )-因此它似乎已启用,并且只有在周围有用户交互时才会“重新绘制”。 "Lazy redraw", so to speak :) 可以这么说:“懒惰重画”

What one can do is, if it happens to be a serious issue, to find a way on one of these computers to get the button visibly disabled with some 'workaround' (eg changing button text after it was disabled -maybe even to exactly the same text-, or move it 1 pixel away and back, call InvalidateRect on it , etc), and apply that solution to your Excel macro. 什么人可以做的是,如果它恰好是一个严重的问题,找到这些计算机上的一个办法让按钮明显一些“处理方法”已禁用(例如,改变按钮上的文字后,这是禁用的-也许甚至完全相同的文本,或将其前后移1个像素,对其调用InvalidateRect等),然后将该解决方案应用于您的Excel宏。


UPDATE after reading asker's comment: 阅读了问者的评论后的更新
Oh I see. 哦,我明白了。 This could happen if the buttons are in a control array . 如果按钮在控件数组中,则可能发生这种情况。 The issue in this case, however should be present on all computers... Strange. 但是,这种情况下的问题应该出现在所有计算机上。
Anyway. 无论如何。 Try to test the buttons' Index property to see if they are part of a control array, just to be sure. 请确保尝试测试按钮的Index属性,以查看它们是否属于控件数组。 If yes: 如是:
a) remove them from the array and create buttons "independent" from each other a)从数组中删除它们并创建彼此“独立”的按钮
b) create a bool array and store there enabled/disabled status and write a function that updates buttons' text color property & handles button click event ignore/accept b)创建一个布尔数组并在其中存储启用/禁用状态,并编写一个函数来更新按钮的文本颜色属性并处理按钮单击事件忽略/接受

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

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