我尝试复制第二个答案 from this question 。这是我的 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
根据上面链接中的答案,这应该使我能够在同一项目的另一个模块中执行此操作:
ThisWorkbook.UserInterface.Show
上下文
我想创建一个无模式用户窗体,其中实例的“生存”时间与工作簿一样长。我听说这可能会导致问题,但没有具体的消息。我想这样做是为了保留我的 UIModel。
UIModel
是模型类 UIFrm
无模式 UI 用户表单
问题
我无法使用 ThisWorkBook.UserInterFace
访问 Costum 属性,如果我将 Property Get
声明为 Public,我将收到有关以下内容的编译错误: Private Properties not能够被公开访问。
是否可以在 ThisWorkbook
-Object 中拥有一个 Property Get ?
最佳答案
ThisWorkbook
可能不是您想象的那样。
有 Application.ThisWorkbook
,它是 Application
对象的属性。它返回调用此属性的代码所在的工作簿。
还有“只是”ThisWorkbook
,a code name代表您的代码所在的 Workbook 类实例。问题在于,此 ThisWorkbook
与 Application.ThisWorkbook
不同,需要进行本地化。它不是对象模型的一部分,它是一个任意名称。
对于英文版 Excel,本地化名称也恰好是 ThisWorkbook
。
对于非英语 Excel,情况会有所不同。
因此,由于范围优先,ThisWorkbook
在英语 Excel 中解析为“代码名称”,在非英语 Excel 中解析为 Application.ThisWorkbook
。
它们是同一个对象,但 Friend 属性只能“真正在内部”访问,因此当通过 Application.ThisWorkbook
路由访问工作簿时,您无法看到它们。
解决方法是在项目树中将本地化的 ThisWorkbook
对象重命名回 ThisWorkbook
。与工作表代码名称一样,此更改是文件本地的。
在代码中使用本地化名称代替 ThisWorkbook
也可以。
关于excel - 在 VBA 中可以在 ThisworkBook 中获取 Friend 属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52874115/