简体   繁体   English

Excel:需要从 VBA 宏加载 Power Query 加载项

[英]Excel: require Power Query add-in load from VBA macro

The scenario is a Windows Server 2012 R2, 64 bit;场景是 Windows Server 2012 R2,64 位; Excel 2010, 32 bit. Excel 2010,32 位。 Many users, just a couple of them have administrative privileges.许多用户,其中只有几个拥有管理权限。 I installed Power Query from the built-in administrator.我从内置管理员安装了 Power Query。 Without asking anything, the add-in got installed for all users;无需询问,就为所有用户安装了插件; I mean that its settings may be found in a registry key under HKLM, not HKCU.我的意思是它的设置可以在 HKLM 下的注册表项中找到,而不是 HKCU。

The key is关键是

HKEY_LOCAL_MACHINE -> SOFTWARE -> Wow6432Node -> Microsoft -> Office -> Excel -> AddIns -> Microsoft.Mashup.Client.Excel

and the relevant value is相关值是

LoadBehavior (REG_DWORD)

Now just a few users really need Power Query.现在只有少数用户真正需要 Power Query。 I don't want it to load for everybody, everytime Excel is launched.我不希望它为每个人加载,每次启动 Excel 时。 I tried some different settings for the LoadBehavior value (see this link).我为 LoadBehavior 值尝试了一些不同的设置(请参阅链接)。 I found the following:我发现了以下内容:

  • If LoadBehavior = 2, the add-in is not loaded for any user, no way.如果 LoadBehavior = 2,则不会为任何用户加载加载项,不可能。
  • If LoadBehavior = 3, the add-in is loaded for all users.如果 LoadBehavior = 3,则为所有用户加载加载项。 However, if I create a specific key for a specific user (in the registry), the user can disable the add-in from Excel.但是,如果我为特定用户(在注册表中)创建特定键,则用户可以从 Excel 禁用加载项。 Eg, in HKEY_CURRENT_USER, the path for the key is:例如,在 HKEY_CURRENT_USER 中,密钥的路径是:

Software -> Microsoft -> Office -> Excel -> Addins -> -Microsoft.Mashup.Client.Excel软件 -> Microsoft -> Office -> Excel -> 插件 -> -Microsoft.Mashup.Client.Excel

  • More specifically, it is sufficient to put in the user's key the LoadBehavior value, and this allows the user to decide whether he wants the add-in loaded or not.更具体地说,将 LoadBehavior 值放入用户的键就足够了,这允许用户决定是否要加载加载项。 A value of 3 means "Load", a value of 2 means "No load".值为 3 表示“负载”,值为 2 表示“无负载”。
  • I need exactly the opposite: add-in normally not loaded, and some users allowed to load it.我需要的恰恰相反:加载项通常未加载,并且某些用户允许加载它。 I found that it is possible to set the LoadBehavior in the HKLM to 9. This means "Load on demand".我发现可以将 HKLM 中的 LoadBehavior 设置为 9。这意味着“按需加载”。 Ie, the add-in is only loaded when the user requires an action depending on the add-in itself.即,仅当用户需要依赖于加载项本身的操作时才加载加载项。 Quite fine for me.对我来说很好。
  • At this point I may leave out the key for specific users (in HKCU, eg).在这一点上,我可能会忽略特定用户的密钥(例如在 HKCU 中)。 However, I found out that if I create it, it takes precedence over the HKLM (when the latter has LoadBehavior = 9).但是,我发现如果我创建它,它优先于 HKLM(当后者的 LoadBehavior = 9 时)。 So it is possible to decide, for specific users, to have the add-in on "Always Load" mode.因此,对于特定用户,可以决定让加载项处于“始终加载”模式。 Just set LoadBehavior = 3 in the key under HKCU.只需在 HKCU 下的键中设置 LoadBehavior = 3。

All this seems fine.这一切似乎都很好。 Now the problem is that I need to call some Power Query actions from a VBA procedure.现在的问题是我需要从 VBA 过程调用一些 Power Query 操作。 If Power Query is already loaded, all is fine.如果 Power Query 已加载,则一切正常。 But if it is not loaded, even with the "Load on demand" setting, the action fails.但如果没有加载,即使设置了“按需加载”,操作也会失败。 In order to get Power Query loaded, one has to press some button on the Excel GUI which calls a Power Query action.为了加载 Power Query,必须按下 Excel GUI 上的某个按钮来调用 Power Query 操作。

I found that there is a property of the add-in object available in VBA which indicates whether the add-in is loaded, and may be set to load or unload it from VBA.我发现 VBA 中有一个可用的加载项对象属性,它指示加载项是否已加载,并且可以设置为从 VBA 加载或卸载它。 It is:这是:

Application.COMAddIns.Item("Microsoft.Mashup.Client.Excel").Connect

If it is True , the add-in is loaded, and if it is False , the add-in is unloaded.如果为True ,则加载加载项,如果为False ,则卸载加载项。

Now it should be possible to load the add-in just by setting this property to True.现在应该可以通过将此属性设置为 True 来加载加载项。 However, this is not the case in my scenario: the result is an error (80004005).但是,在我的场景中情况并非如此:结果是错误 (80004005)。 This seems a problem related to the user not having administrative privileges.这似乎是与没有管理权限的用户有关的问题。 See this page - this behavior is considered a bug.请参阅页面 - 此行为被视为错误。

My last idea, which I will try later, is to completely remove the LoadBehavior in the key under HKLM.我的最后一个想法,我稍后会尝试,是完全删除 HKLM 下密钥中的 LoadBehavior。 I already checked out that this prevents users from seeing the add-in, unless the user-specific key is created, in which case the user can set the add-in load behavior autonomally.我已经检查出这会阻止用户看到加载项,除非创建了用户特定的密钥,在这种情况下,用户可以自动设置加载项加载行为。 I will see what happens in this case when the load is requested from VBA.当从 VBA 请求加载时,我将看到在这种情况下会发生什么。

Meanwhile, I'd appreciate any idea to solve the thing: having Power Query not loaded normally, possibly available on demand for all users, loaded automatically from VBA (at least for some users), and all this without having to manually add the user-specific key for all users.同时,我很感激解决这个问题的任何想法:让 Power Query 没有正常加载,可能按需为所有用户提供,从 VBA 自动加载(至少对于某些用户),所有这些都无需手动添加用户- 所有用户的特定密钥。 It is acceptable to add this key for a few users, those who actually need Power Query.为少数用户添加此键是可以接受的,这些用户实际上需要 Power Query。

EDIT编辑

Removing, or renaming, the LoadBehavior value in the key under HKLM works.删除或重命名 HKLM 下项中的 LoadBehavior 值。 Power Query is then only seen by users who have a specific key under HKCU.只有在 HKCU 下拥有特定密钥的用户才能看到 Power Query。 If, in this key, the value of LoadBehavior is set to 3 (or 2), then the add-in is loaded by default (respectively, not loaded).如果在此键中将 LoadBehavior 的值设置为 3(或 2),则加载项默认加载(分别为未加载)。 The VBA instruction to change the .Connect property works fine;更改 .Connect 属性的 VBA 指令工作正常; it switches the LoadBehavior between 3 (True) and 2 (False).它在 3(真)和 2(假)之间切换 LoadBehavior。 Luckily, I can also set LoadBehavior = 9 in the registry (under HKCU), and the .Connect property is still writable.幸运的是,我还可以在注册表中(在 HKCU 下)设置 LoadBehavior = 9,并且 .Connect 属性仍然是可写的。 In this situation, when this property is assigned a True value the add-in is loaded, but the LoadBehavior value stands still at 9, so that upon closing and re-opening Excel the add-in is unloaded, is set as "Load on demand", and may be loaded from VBA.在这种情况下,当此属性被分配一个 True 值时,加载项被加载,但 LoadBehavior 值仍为 9,因此在关闭并重新打开 Excel 时加载项被卸载,设置为“加载需求”,并且可以从 VBA 加载。

This is exactly the behavior I was looking for;这正是我正在寻找的行为; the only caveat is that the key needs to be created for all users who need Power Query.唯一需要注意的是,需要为所有需要 Power Query 的用户创建密钥。 Since in my situation they may be counted on one hand's fingers, this solution is acceptable.因为在我的情况下,它们可能是用一只手的手指来数的,所以这个解决方案是可以接受的。

I'm still curious to see whether anyone comes out with any better solution.我仍然很想知道是否有人提出了更好的解决方案。

Have you considered using a powershell script to update the registry entries for each user accordingly?您是否考虑过使用 powershell 脚本来相应地更新每个用户的注册表项? The following example is a bit complex, line 111 has the loop you need to set it for many users.下面的例子有点复杂,第111行有你需要为很多用户设置的循环。

https://daniel.streefkerkonline.com/2014/04/09/re-enable-microsoft-office-add-ins-with-powershell/ https://daniel.streefkerkonline.com/2014/04/09/re-enable-microsoft-office-add-ins-with-powershell/

My other recommendation is you should not use VBA in Excel 2010 to call anything PowerQuery related.我的另一个建议是您不应在 Excel 2010 中使用 VBA 来调用任何与 PowerQuery 相关的内容。 Given how it is an add-in I don't think it's included in the service agreements for Excel 2010. VBA interaction with PowerQuery was not fully supported in Excel 2010 or debugged.鉴于它是一个加载项,我认为它没有包含在 Excel 2010 的服务协议中。Excel 2010 中不完全支持 VBA 与 PowerQuery 的交互或调试。 I noticed in several patches broke the GUI in PowerQuery altogether.我注意到有几个补丁完全破坏了 PowerQuery 中的 GUI。 Upgrading to Excel 2013 or 2016 was the fix.升级到 Excel 2013 或 2016 是解决方法。

When using Excel 2016 VBA editor to interact with PowerQuery, the macro code is challenging to debug and get working, and its references are poorly documented.使用 Excel 2016 VBA 编辑器与 PowerQuery 进行交互时,宏代码很难调试和运行,而且其引用记录不完整。 It's more stable to show you an error and not just crash but it's very challenging to troubleshoot the errors the compilers find.向您显示错误更稳定,而不仅仅是崩溃,但对编译器发现的错误进行故障排除非常具有挑战性。

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

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