朋友 属性 可以在 VBA 中加入 ThisworkBook 吗?
Is a Friend Property Get in ThisworkBook possible in VBA?
我尝试复制第二个答案。这是我的 ThisWorkBook 代码:
代码
Option Explicit
Private Type TView
UserInterFace As UIFrm
Model as UIModel
End Type
Private this As TView
Friend Property Get UserInterFace() As UIFrm
If this.UserInterFace Is Nothing Then
Set this.UserInterFace = New UIFrm
End If
Set UserInterFace = this.UserInterFace
End Property
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not this.UserInterFace Is Nothing Then
Unload this.UserInterFace
Set this.UserInterFace = Nothing
End If
End Sub
这应该 - 根据上面 link 中的答案 - 让我能够在同一项目的另一个模块中执行此操作:
ThisWorkbook.UserInterface.Show
上下文
我想创建一个无模式用户窗体,其中的实例 "lives" 与工作簿一样长。我听说这可能会导致问题,但没有什么具体的。我想这样做是为了保留我的 UI 模型。
UIModel
是模型 Class UIFrm
无模型 UI 用户表单
问题
我无法使用 ThisWorkBook.UserInterFace
访问服装 属性,如果我将 Property Get
声明为 Public,我将收到有关以下内容的编译错误:私有属性无法公开访问。
这甚至有可能 属性 进入 ThisWorkbook
-Object 吗?
ThisWorkbook
可能不是你想的那样。
有Application.ThisWorkbook
,是Application
对象的属性。它 returns 调用此 属性 的代码所在的工作簿。
还有 "just" ThisWorkbook
,一个 表示您的代码所在的工作簿 class 实例。问题是这个 ThisWorkbook
与 Application.ThisWorkbook
不同,需要本地化。它不是对象模型的一部分,它是一个任意名称。
对于Excel的英文版,本地化名称恰好也是ThisWorkbook
。
对于非英语 Excels,它会有所不同。
因此,由于范围优先,ThisWorkbook
在英语 Excel 中解析为 "the code name",在非英语 Excel 中解析为 Application.ThisWorkbook
。
它们是同一个对象,但是 Friend 属性只能访问 "truly internally",所以当通过 Application.ThisWorkbook
路由访问工作簿时,您看不到它们。
解决方法是在项目树中将本地化的 ThisWorkbook
对象重命名回 ThisWorkbook
。与 sheet 代号一样,此更改是文件本地的。
在您的代码中使用本地化名称而不是 ThisWorkbook
也可以。
我尝试复制第二个答案
代码
Option Explicit
Private Type TView
UserInterFace As UIFrm
Model as UIModel
End Type
Private this As TView
Friend Property Get UserInterFace() As UIFrm
If this.UserInterFace Is Nothing Then
Set this.UserInterFace = New UIFrm
End If
Set UserInterFace = this.UserInterFace
End Property
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not this.UserInterFace Is Nothing Then
Unload this.UserInterFace
Set this.UserInterFace = Nothing
End If
End Sub
这应该 - 根据上面 link 中的答案 - 让我能够在同一项目的另一个模块中执行此操作:
ThisWorkbook.UserInterface.Show
上下文
我想创建一个无模式用户窗体,其中的实例 "lives" 与工作簿一样长。我听说这可能会导致问题,但没有什么具体的。我想这样做是为了保留我的 UI 模型。
UIModel
是模型 Class UIFrm
无模型 UI 用户表单
问题
我无法使用 ThisWorkBook.UserInterFace
访问服装 属性,如果我将 Property Get
声明为 Public,我将收到有关以下内容的编译错误:私有属性无法公开访问。
这甚至有可能 属性 进入 ThisWorkbook
-Object 吗?
ThisWorkbook
可能不是你想的那样。
有Application.ThisWorkbook
,是Application
对象的属性。它 returns 调用此 属性 的代码所在的工作簿。
还有 "just" ThisWorkbook
,一个 ThisWorkbook
与 Application.ThisWorkbook
不同,需要本地化。它不是对象模型的一部分,它是一个任意名称。
对于Excel的英文版,本地化名称恰好也是ThisWorkbook
。
对于非英语 Excels,它会有所不同。
因此,由于范围优先,ThisWorkbook
在英语 Excel 中解析为 "the code name",在非英语 Excel 中解析为 Application.ThisWorkbook
。
它们是同一个对象,但是 Friend 属性只能访问 "truly internally",所以当通过 Application.ThisWorkbook
路由访问工作簿时,您看不到它们。
解决方法是在项目树中将本地化的 ThisWorkbook
对象重命名回 ThisWorkbook
。与 sheet 代号一样,此更改是文件本地的。
在您的代码中使用本地化名称而不是 ThisWorkbook
也可以。