简体   繁体   English

如何使用VBA识别ThisWorkbook模块

[英]How to identify ThisWorkbook module using VBA

Using Excel 2010. 使用Excel 2010。

I need to add code to a remote Excel file where ThisWorkbook module has been renamed, let's say to "DashboardWorkbook". 我需要将代码添加到已重命名ThisWorkbook模块的远程Excel文件中,让我们说“DashboardWorkbook”。 I don't know however the new name, can be anything but I need to identify this module programmatically in order to add more code to Sub Workbook_Open(). 我不知道新名称,可以是任何东西,但我需要以编程方式识别此模块 ,以便向Sub Workbook_Open()添加更多代码。

I am opening this remote file, then I go through all it's components: 我打开这个远程文件,然后我浏览它的所有组件:

Private Sub AddProcedureToWorkbook(wb As Workbook)
  Dim VBProj As VBIDE.VBProject
  Dim oComp As VBIDE.VBComponent
  Dim oCodeMod As VBIDE.CodeModule

  Set VBProj = wb.VBProject 
  For Each oComp In VBProj.VBComponents
    If *[check here if oComp was formerly ThisWorkbook but now renamed]* Then
      Set oCodeMod = oComp.CodeModule
        'add new code here
      ...etc, etc
    End If
  Next

End Sub

In Excel interface, ThisWorkbook has a different icon so it seems to be a different module type but I could not figure out what specific property to read in order to identify it? 在Excel界面中,ThisWorkbook有一个不同的图标,所以它似乎是一个不同的模块类型,但我无法弄清楚要识别的具体属性是什么?

To complicate things even more, sometimes Sub Workbook_Open() doesn't exist, therefore I need to add it at the right place... 为了使事情复杂化,有时Sub Workbook_Open()不存在,因此我需要在正确的位置添加它...

Thank you, 谢谢,

MR 先生

Sheets and books can be accessed directly from code by their CodeName (different from display name aka just Name ). 可以通过CodeName直接从代码访问表格和书籍(不同于显示名称,也就是Name )。
This is also their VBComponent name. 这也是他们的VBComponent名称。

Private Sub AddProcedureToWorkbook(wb As Workbook)
  Dim VBProj As VBIDE.VBProject
  Dim oComp As VBIDE.VBComponent
  Dim oCodeMod As VBIDE.CodeModule

  Set VBProj = wb.VBProject
  Set oComp = VBProj.VBComponents(wb.CodeName)
  Set oCodeMod = oComp.CodeModule

  oCodeMod.AddFromString "sub Hi()" & vbNewLine & "msgbox ""Hi.""" & vbNewLine & "end sub"
End Sub

Each of the VBProj.VBComponents items has a Properties collection. 每个VBProj.VBComponents项都有一个Properties集合。 The set of properties of the Workbook object is different to the others (Sheets, Modules etc). Workbook对象的属性集与其他对象(表格,模块等)不同。

Pick a unique property of the Workbook and search the components collection for that. 选择工作簿的唯一属性并搜索组件集合。

Try this 试试这个

Private Function FindThisWorkbook(wb As Workbook) As VBIDE.VBComponent
    Dim VBProj As VBIDE.VBProject
    Dim oComp As VBIDE.VBComponent
    Dim oP As Property

    Set VBProj = wb.VBProject
    For Each oComp In VBProj.VBComponents
        Set oP = Nothing
        On Error Resume Next
        Set oP = oComp.Properties("ActiveSheet")
        On Error GoTo 0
        If Not oP Is Nothing Then
            ' Found it
            Set FindThisWorkbook = oComp
            Exit Function
        End If
    Next

End Function

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

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