如何在输入该文本框时更改表单字段文本框的文本 [英] How to change the text of a form field text box on entry of that text box

查看:173
本文介绍了如何在输入该文本框时更改表单字段文本框的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在输入表单字段文本框时更改其文本.我选择在条目上运行一个宏,但似乎无法使用该宏为我的所有字段动态更改它.有数百个字段是动态删除和添加的,因此为每个字段设置书签不是此问题答案中指定的选项

I am trying to change the text of a form field text box when it is entered into. I selected to run a macro on entry, but I can't seem to change it dynamically for all my fields using that macro. There are hundreds of fields which are removed and added dynamically, so setting a bookmark for every one of them is not an option as specified in this question answer MS Word Document Form - Check Value of Text Form Field on Exit

在输入时有什么方法可以访问表单字段,以便他们输入时可以更改它?

Is there any way to access the form field on entry so I can change it when they enter it?

我尝试通过书签更改它,但是对我来说,这不是一个选择:

I have tried changing it by bookmark, but again this isn't an option for me:

ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Result = "my value"

我已经尝试了Selection.FormFields(1)... etc,但是它不允许我在输入时访问它.还有其他方法吗?

I have tried Selection.FormFields(1)...etc but it does not let me access this on entry. Any other ways?

我想在输入时访问表单字段以更改值,但无法弄清楚该怎么做!

I am wanting to access the form field on entry to change the value, but cannot figure out how to do it!

推荐答案

默认情况下,每个表单字段都有一个书签和一个名称.它们是由Word在插入表单字段并且文本字段的名称遵循Text1,Text2等模式时生成的.

Every form field has a bookmark and a name, by default. They're generated by Word when the form field is inserted and the names for text fields follow the pattern Text1, Text2, and so on.

虽然Selection在触发输入时"宏时未返回表单字段,但确实返回了书签.通过此操作,可以获得书签名称,然后从那里访问表单字段.所以:

While Selection doesn't return the form field when an "On Enter" macro is triggered it does return the bookmark. Through that, it's possible to get the bookmark name and from there access the form field. So:

Sub WriteValueToFormFieldOnEnter()
    Dim bkm As Word.Bookmark
    Dim ffldName As String, ffld As Word.FormField

    If Selection.Bookmarks.Count > 0 Then
        Set bkm = Selection.Bookmarks(1)
        ffldName = bkm.Name
        Set ffld = ActiveDocument.FormFields(ffldName)
        ffld.result = "New"
    End If    
End Sub

注意:如果表单域没有书签名称,因为它们是在同一文档中复制/粘贴的,则必须将名称分配给表单域. (书签名称在文档中必须是唯一的,这就是这种情况的原因.)以下代码循环了文档中的表单域,并为没有名称的表单域分配了名称.

Note: If the form fields have no bookmark name because they were copied/pasted within the same document, then names have to be assigned to the form fields. (Bookmark names must be unique within a document, which is why this happens.) The following code loops the formfields in a document and assigns those with no name a name.

FormField.Name状态的语言参考

返回一个字符串,该字符串表示指定表单字段的结果.读/写.

Returns a String that represents the result of the specified form field. Read/write.

写入此属性会导致错误(至少在Word 2010和更高版本中,也许在旧版本中也是如此).因此,有必要使用Word UI的表单字段属性"对话框.

writing to this property causes an error (at least in Word 2010 and newer, perhaps also in older versions). So it's necessary to work with the Word UI's Form Field Properties dialog box.

Sub NameUnnamedFormFields()
    Dim ffld As Word.FormField
    Dim rng As Word.Range
    Dim tbCounter As Long, cbCounter As Long, ddCounter As Long

    tbCounter = 1: cbCounter = 1: ddCounter = 1
    For Each ffld In ActiveDocument.FormFields
        If Len(ffld.Name) = 0 Then
            ffld.Select
            With Application.Dialogs(wdDialogFormFieldOptions)
                Select Case ffld.Type
                    Case wdFieldFormTextInput
                        .Name = "myText" & CStr(tbCounter)
                        tbCounter = tbCounter + 1
                    Case wdFieldFormCheckBox
                        .Name = "myCheckBox" & CStr(cbCounter)
                        cbCounter = cbCounter + 1
                    Case wdFieldFormDropDown
                        .Name = "myDropDown" & CStr(ddCounter)
                        ddCounter = ddCounter + 1
                    Case Else
                        MsgBox "Unknown form field type."
                End Select
                .Execute
            End With
        End If
    Next
End Sub

这篇关于如何在输入该文本框时更改表单字段文本框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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