在 Excel 和 Word 中将用户窗体作为加载项安装时遇到问题

Trouble to install a userform as an add-in in Excel and Word

我创建了一个 vba 用户表单,当我想从 exel 或 word 中打印某些内容时,它会出现。但是我无法将其作为加载项安装(因此它将适用于我尝试打印的所有办公文档)。

我发现了一个类似的问题,并使用了那里提到的安装代码,但它给我一个错误。

这是link:How to create a macro that will open a userform from an Add-in without opening the workbook

这里是错误:

An unexpected error has occured, please contact CSC DM

Design with the bellow error details.

Module = UserfulGeneric Code

Procedure = CreateMMMacroMenu

Line = 0

Error Code = 91

Error Text = Object veriable or With block variable not set

我能做些什么来解决这个问题吗?我是 vba 的新手,非常感谢任何帮助。

事实证明对于 Excel:

非常简单

为了演示,使用 ThisWorkbook.IsAddin = True、用户表单和代码创建了一个文件 TestAddIn.xlamThisWorkbook 模块中。


ThisWorkbook 模块中的代码:

Option Explicit

Private WithEvents App As Application

Private Sub App_WorkbookBeforePrint(ByVal Wb As Workbook, Cancel As Boolean)
    'Debug.Print Now & " " & "App_WorkbookBeforePrint: " & Wb.FullName
    UserForm1.Show
End Sub

Private Sub Workbook_Open()
    Set App = Application
    'Debug.Print Now & " " & "Loading " & ThisWorkbook.FullName
End Sub


用户窗体1:


代码:

Option Explicit

Private Sub UserForm_Initialize()
    Me.TextBox1.Text = ActiveWorkbook.FullName
End Sub


现在确保您通过 Excel UI 正常添加此插件,或使用 VBA 安装到 Excel。但是之后你需要重新启动Excel。

打印新工作簿时:用户表单在实际打印之前出现。