Excel Interop C#无法在Workbook.VBProject.VBComponents.Add(..)上添加VBProject错误800A03EC [英] Excel Interop C# Can't add VBProject Error 800A03EC on Workbook.VBProject.VBComponents.Add(..)

查看:254
本文介绍了Excel Interop C#无法在Workbook.VBProject.VBComponents.Add(..)上添加VBProject错误800A03EC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I)全局说明

你好

我正在使用Microsoft.Office.Interop.Excel以C#开发应用程序.就我而言,我实现了一些旧文件宏,这些旧文件宏存储在文件中到由C#打开的Excel工作簿中,以升级它们而无需在我的应用程序中重新编码宏.

I'm developing an app in C# using Microsoft.Office.Interop.Excel. In my case I implement some olds macros stored in a file to an Excel Workbook, opened by C#, to upgrade them without re-coding macros in my app.

在全球范围内,我的应用中都有我的流程:

Globally there is my process in my app :

  • 用户选择一个更新的宏,
  • 我检查宏是否需要一些东西:
    • 在这里,我将寄存器键CURRENT_USER \ SOFTWARE \ Microsoft \ Office \ [office_ID] .0 \ Excel \ Security \ AccessVBOM设置为1,以允许宏/VBProject修改
    • The user select an updated macro,
    • I check if the macro require something :
      • Here I set the register Key CURRENT_USER\SOFTWARE\Microsoft\Office\[office_ID].0\Excel\Security\AccessVBOM to 1 for allows macro/VBProject modifications

      II)我的问题和研究结果

      在我将存储在文件中的宏添加到工作簿"部分中,当我使用以下指令将VBProject添加到工作簿(需要添加宏)时,出现错误800A03EC.

      In the "I add my macro, stored in a file, to the workbook" part, when I used the following instruction to add a VBProject to the workbook (required to add the macro) I get an error 800A03EC.

      /// wb is a workbook object
      VBComponent module = wb.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
      

      此错误与一条消息一起发送(严重交易):不信任对VB项目的访问."

      This error is send with a message (grossly traduce) : "The access to VB Project are not trust."

      正如我在Internet上看到的那样,如果未正确设置Excel中的选项,则会发生此错误.可以使用CURRENT_USER \ SOFTWARE \ Microsoft \ Office \ [office_ID] .0 \ Excel \ Security \ AccessVBOM注册键来处理此选项. 为了避免这个问题,我正在使用注册密钥管理来构建我的应用程序.

      As I see on the Internet, this error occurs when an option in Excel isn't correctly set. This option can by handle with the CURRENT_USER\SOFTWARE\Microsoft\Office\[office_ID].0\Excel\Security\AccessVBOM register key. And I'm building my app with a register key management for avoid this problem.

      正如您将在调试显示部分看到的那样,我们可以看到在执行过程中正确设置了该选项.

      And as you will see on the debug display part, we can see the option is correctly set on the execution.

      我的问题 :是否还有另一种情况发生此错误?

      My question : there is another case when this error occurs?

      III)调试分析

      当我捕获ComException时,将使用以下内容显示寄存器键值.

      When I catch the ComException, I use the following to display registers keys values.

      /// RequirementManager is my own class, use to set and unset endpoints computers parameters, before and after each user action
      RegistryKey reg;
      for (int i = RequirementManager.EXCEL_ID_HANDLED_MIN; i < RequirementManager.EXCEL_ID_HANDLED_MAX; i++)
      {
          reg = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Office\" + i + @".0\Excel\Security\", RegistryKeyPermissionCheck.ReadSubTree);
          Debug.WriteLine(String.Format(@"Value of CURRENT_USER\SOFTWARE\Microsoft\Office\{0}.0\Excel\Security\AccessVBOM = {1}", i, reg.GetValue("AccessVBOM")), "Debug");
          reg.Close();
      }
      reg = null;
      

      这是此行的控制台输出:

      And here is the console output of this lines :

      Debug: Value of CURRENT_USER\SOFTWARE\Microsoft\Office\11.0\Excel\Security\AccessVBOM = 1
      Debug: Value of CURRENT_USER\SOFTWARE\Microsoft\Office\12.0\Excel\Security\AccessVBOM = 1
      Debug: Value of CURRENT_USER\SOFTWARE\Microsoft\Office\13.0\Excel\Security\AccessVBOM = 1
      Debug: Value of CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Excel\Security\AccessVBOM = 1
      Debug: Value of CURRENT_USER\SOFTWARE\Microsoft\Office\15.0\Excel\Security\AccessVBOM = 1
      Debug: Value of CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Excel\Security\AccessVBOM = 1
      

      如您所见,错误发生时正确设置了该选项.并在修改注册表项之前关闭所有Excel实例,以免出现问题.我使用类似的算法来修改此范围的注册码,就像一个懒惰的男孩一样.

      As you can see, the option is correctly set when the error occurs. And all instance of Excel are closed before the modification of register keys, for avoid problem. I use similar algorithm to modify this range of register keys, like a lazy boy.

      欢迎提出任何想法

      Any idea are welcome

      关于Dykoine,

      IV)回答反应

      @ prizm1此参数是否允许使用宏(显示橙色警告消息),并且在我的计算机上(测试时)将其设置为允许所有宏.

      @prizm1 This parameter if macro are allowed or not (display orange warning message), and on my computer (while test) I set it to allow all macro.

      经过测试,我可以看到此选项不能保护用户,因为即使您使用禁用所有功能而没有通知",我们也可以使用C#打开Excel文件并在其中执行宏,而不会被该选项阻止.

      After test, I can see this option doesn't protect the user, because we can open an Excel file with C# and execute a macro in it without be block by this option even if you use "disabled all without notification".

      推荐答案

      这里的代码在将VBA放入Excel文件中没有任何问题.也许您需要在输入代码之前先将其保存到磁盘,因为该项目由文件名决定.只是一个补充,没有真正的解决方案:

      Here the code which haven't made any problems with putting VBA into an Excel file. Maybe you need to save it to disk first before entering the Code, since the Project is determined by the filename. Just an addition, and no real solution:

          public bool injectVBA(String scriptText, String target)
          {
              VBProject found = null;
              Access.Application currApplication = this.currentInstance.Application;
              if (target.Equals("") || scriptText.Equals(""))
                  return false;
              foreach (VBProject vb in currApplication.VBE.VBProjects)
              {
                  if (currApplication.CurrentDb().Name.Equals(vb.FileName))
                  {
                      found = vb;
                      break;
                  }
              }
      
              if (found != null)
              {
                  foreach (VBComponent foundComponent in found.VBComponents) 
                  {
                      if (foundComponent.Name.Equals(target))
                      {
                          return true;
                      }
                  }
                  VBComponent module = found.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
                  module.Name = target;
                  module.CodeModule.AddFromString(scriptText);
                  module.Activate();
                  //currApplication.DoCmd.OpenModule(target, Type.Missing);
                  currApplication.DoCmd.Save(Access.AcObjectType.acModule, target);
                  return true;
              }
              else
              {
                  return false;
              }
          }
      

      这篇关于Excel Interop C#无法在Workbook.VBProject.VBComponents.Add(..)上添加VBProject错误800A03EC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆