通过单击关闭 Word 中的表单 (VBA) [英] Close Forms in Word by clicking away (VBA)

查看:69
本文介绍了通过单击关闭 Word 中的表单 (VBA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VBA 新手,有一个包含多个 ActiveX 按钮的文档,每个按钮打开一个带有一些信息文本的表单.用户应该能够通过点击离开"来关闭窗口,而不必点击每个窗口的x".

I am new to VBA and have a document with multiple ActiveX buttons that each open a form with some infotext. Instead of having to click the 'x' of each window the user should be able to close a window by 'clicking away'.

(可能有更好的方法来显示信息框",但 MsgBox 更糟糕,因为它还会触发声音警报.我选择了 ActiveX 按钮,因为可以轻松加载图像)

(There may be better ways to display 'infoboxes', but the MsgBox is even worse since it also triggers a sound alert. I chose the ActiveX button bc a image can be loaded easily)

推荐答案

这个有效

经过广泛的研究(但仍然没有真正的经验),我想出了这个解决方案:

This works

After extensiv research (but still no real expierience) I came up with this solution:

  1. 插入 ActiveX 按钮,右键单击并选择查看代码"

  1. Insert ActiveX Button, right click and select 'view code'

您应该被定向到相应单击子中ThisDocument"下的 VBA.在此创建一个新表单(参见步骤 3)并将该表单显示到窗口.使用 0 是必需的,否则窗口以后不会关闭.第 4 步和第 5 步需要第二个 Sub.

You should be directed to the VBA under 'ThisDocument' in the corresponding click-Sub. Here create a new Form (see Step 3) and show the Form to windows. Using 0 is required, else the window won't be shut later. The second Sub is needed for step 4 and 5.

 Private Sub cmdHelp_Click()
   Dim frm As New frmHelp
   frm.Show 0  ' with 0 it is possible to unload window, with 1 not!
   Set frm = Nothing        
 End Sub

 Private Sub DocumentActivateEvent()
     Call Register_Event_Handler 
 End Sub

  • 创建新表单.表单名称必须是上面指定的frmHelp".现在按下创建的按钮时,将显示此表单.

  • Create new Form. The Form Name must be 'frmHelp' as specified above. Now when the created button is pressed, this form will be displayed.

    要关闭打开的表单,需要一个 eventHandler,即在 Word 屏幕激活(从表单切换到 Word)时触发.要关闭表单,请选择所有表单的循环.将其插入一个新的 Class 'Class1':

    To close open forms a eventHandler is needed, that is triggered, when the Word screen is activated (switch from Form to Word). To close the form, a loop over all Forms is chosen. Insert this into a new Class 'Class1':

     Private WithEvents app As Application
    
     Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
         'Debug.Print "Event occurred!"
         Dim frm As UserForm
    
         For Each frm In UserForms
             If frm.Enabled Then
                 Unload frm  'form is unloaded
             End If
         Next frm
     End Sub   
    
     Private Sub Class_Initialize()    
         Set app = Application
     End Sub
    
     Private Sub Class_Terminate()    
         Set app = Nothing
     End Sub
    

  • 创建一个新的模块'modEventHandler1':

  • Create a new Module 'modEventHandler1' with:

     Private X As Class1
    
     Sub Register_Event_Handler()
         Set X = New Class1
     End Sub
    
     Sub Unregister_Event_Handler()
         Set X = Nothing
     End Sub
    

  • 应该可以!重新启动 word 或手动执行Register_Event_Handler"Makro.现在,当第 2 步中的DocumentAcrivateEvent"被触发时,EventHandler 被初始化.希望我能帮上忙.

    That should do it! Restart word or manually execute the 'Register_Event_Handler' Makro. Now, when the 'DocumentAcrivateEvent' from step 2. is triggered, the EventHandler is initialised. Hope I could help.

    这篇关于通过单击关闭 Word 中的表单 (VBA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆