Word vba - 如果光标位于单元格/书签中 - 运行代码

发布于 2024-07-25 00:19:54 字数 225 浏览 2 评论 0原文

我有一个模板文档,其中包含几个部分和一些表格。 问题是,我试图在表中的单元格内插入一个下拉列表。 为了使下拉列表发挥作用,需要保护文档。 但是,如果我保护表所在的整个部分,则整个表都会受到保护。

所以,我想知道是否有一种方法可以在用户单击下拉列表时执行宏代码? 然后,代码将保护文档,使控件实际工作,然后选择一个选项,当用户在字段之外单击时,文档应该不受保护。

这可能吗?

I have a template document which contains several sections and a few tables.
The thing is, I'm trying to insert a drop-down list inside on of the cells in the table.
And for a drop-down list to work, the document needs to be protected. But if I protect the entire section the table is in, the entire table is protected.

So, I was wondering if there's a way of executing a macro code IF the user clicks on the drop-down list? The code would then protect the document, making the control actually work, then select a choice and when the user clicks outside of the field, the document should get unprotected.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-08-01 00:19:54

实际上,Word VBA 中有一个 WindowSelectionChange 事件可供您使用。 Word VBA 帮助文件中的“将事件与应用程序对象一起使用”下对此进行了描述。

诀窍是使用 WithEvents 关键字将您的应用程序分配给类模块(我将其命名为 EventClassModule)中的变量:

Public WithEvents App As Word.Application

然后在普通的 Document Open 事件中,您可以将该变量初始化为当前应用程序:

Dim oEvents As New EventClassModule
Private Sub Document_Open()
    Set oEvents.App = Word.Application
End Sub

返回 EventClassModule,您可以使用 WindowSelectionChange 事件来检查选择是否是表格:

Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
    If Sel.Information(wdWithInTable) And ThisDocument.ProtectionType = wdNoProtection Then
        ThisDocument.Protect wdAllowOnlyFormFields
    ElseIf ThisDocument.ProtectionType <> wdNoProtection Then
        ThisDocument.Unprotect
    End If
End Sub

每当光标更改位置时都会调用此代码。 我测试了它,它有点挑剔(oEvents 对象由于某种原因倾向于未初始化),但希望这将是您解决方案的开始。

There is actually a WindowSelectionChange event in Word VBA that you can use. It is described in the Word VBA help file under "Using Events with the Application Object".

The trick is to assign your application to a variable in a class module (I've named mine EventClassModule) using the WithEvents keyword:

Public WithEvents App As Word.Application

Then in your ordinary Document Open event, you can initialize the variable to the current Application:

Dim oEvents As New EventClassModule
Private Sub Document_Open()
    Set oEvents.App = Word.Application
End Sub

Back in the EventClassModule, you use the WindowSelectionChange event to check if the selection is a table:

Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
    If Sel.Information(wdWithInTable) And ThisDocument.ProtectionType = wdNoProtection Then
        ThisDocument.Protect wdAllowOnlyFormFields
    ElseIf ThisDocument.ProtectionType <> wdNoProtection Then
        ThisDocument.Unprotect
    End If
End Sub

This code will be called whenever the cursor changes location. I tested it and it's a little finicky (the oEvents object has a tendency to become uninitialized for some reason), but hopefully this will be a start for your solution.

撕心裂肺的伤痛 2024-08-01 00:19:54

您可以使用控件工具栏中的组合框,而不是使用表单工具栏中的下拉框。 然后您可以使用 ComboBox 单击事件。 您还可以将代码附加到当用户在 ComboBox 外部单击时的 GotFocus/LostFocus 事件。

Rather than use a drop-down box from the forms toolbar, you could use a ComboBox from the control toolbar. Then you could use the ComboBox click event. You could also attach code to the GotFocus/LostFocus event for when the user clicks outside of the ComboBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文