VBA宏重置Word 2010可填写表单 [英] VBA Macro to Reset a Word 2010 Fillable Form
问题描述
您好,
我使用Tables创建了Word 2010可填写表单。表中的某些单元格具有纯文本内容控件和复选框内容控件。我没有使用Legacy Controls。我包含一个是/否消息框,以帮助用户保存并清除
表单以供将来和持续使用。我有两个问题:1。)我无法弄清楚如何编写一个vba宏来提示用户输入路径/目的地和文件名进行保存; 2.)如何编写一个vba宏来清除表格单元格中
内容控件中的文本和复选框。
I created a Word 2010 fillable form with the use of Tables. Some of the cells in the tables have Plain Text Content Controls and Checkbox Content Controls. I am not using Legacy Controls. I included a Yes/No Message-box to help the user save and clear the form for future and continual use. I have two problems: 1.) I can not figure out how to write a vba macro to prompt the user to enter a path/destination and file name for saving; and 2.) how to write a vba macro to clear the text and checked boxes within the content controls in the cells of the tables.
我所拥有的宏如下:
Private Sub CommandButton1_Click()
content_clear = MsgBox("您要在清除表单之前保存吗? ",vbYesNo + vbQuestion," Content" ;)
如果content_clear = 6则退出Sub '我们不会做任何事情因为用户没有点击
[这里会提示用户提示保存文件]
如果content_clear = 7那么
If content_clear = 7 Then
[这里会去代码删除可填写的表格]
结束如果
结束次
[宏观结束]
消息框的工作原理是Yes和No按钮跟随代码。
The message box works in that the Yes and No buttons follow the code.
我希望有人可以帮助我。
It is my hope that someone can help me.
谢谢,
詹姆斯。
推荐答案
而不是尝试重新使用表单,您应该从它创建一个启用宏的模板(即.dotm文件),所有控件都处于默认状态,然后将其用于新表单。这样,每次你想要一个新表格都没有什么可以"清除"。
此外,当从模板创建文档时,Word提供通过"另存为"将其另存为docx文件,这意味着:(a)覆盖模板的风险最小; (b)你最终没有文件的多个副本,每个副本包含你的宏的b $ b副本(docx文件只是使用模板中的任何宏,只要这些模板仍然可访问)。
Instead of trying to re-use the form, you should create a macro-enabled template from it (i.e. a .dotm file), with all of the controls in their default state, then use that for new forms. That way there's nothing to 'clear' each time you want a new form. Furthermore, when a document is created from a template, Word offers to save it as a docx file, via Save As, meaning: (a) there is minimal risk of overwriting the template; and (b) you don't end up with multiple copies of the document each containing a copy of your macro (docx files simply employ whatever macros are in their templates, provided those templates remain accessible).
也就是说,要重置文档中的所有内容控件,您可以使用以下代码:
That said, to reset all content controls in a document you might use code like:
Application.ScreenUpdating = False
Dim CCtrl As ContentControl
For Each CCtrl In ActiveDocument.ContentControls
With CCtrl
Select Case .Type
Case wdContentControlCheckBox: .Checked = False
'Case wdContentControlDate: .Range.Text = Format(Now, .DateDisplayFormat)
Case wdContentControlPicture: .Range.InlineShapes(1).Delete
Case wdContentControlDropdownList: .Type = wdContentControlText: .Range.Text = "": .Type = wdContentControlDropdownList
Case Else: .Range.Text = ""
End Select
End With
Next
Application.ScreenUpdating = True
您会注意到Date cotnent控件的注释掉的行。如果您激活该行,则内容控件将重置为今天的日期;否则它将被清除。
You'll note a commented-out line for Date cotnent controls. If you activate that line, the content control will be reset to today's date; otherwise it will be cleared.
这篇关于VBA宏重置Word 2010可填写表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!