如何在Word-VBA中创建超过255个字符的可编辑字段?

时间:2017-02-22 10:47:27

标签: vba word-vba

我正在尝试在Word文档中创建可编辑字段:

Dim bm As Bookmark

If ActiveDocument.Bookmarks.Exists(g_name) = True Then
    Set bm = ActiveDocument.Bookmarks(g_name)
End If

If g_var = "DETAILS" Then
    bm.Range.Select
    With Selection
     .Font.Underline = wdUnderlineNone
     .FormFields.Add Range:=Selection.Range, Type:= _
         wdFieldFormTextInput
         Selection.PreviousField.Select
         With Selection.FormFields(1)
         .Name = g_name
         .EntryMacro = ""
         .ExitMacro = ""
         .Enabled = True
         .OwnHelp = False
         .HelpText = ""
         .OwnStatus = False
         .StatusText = ""
         With .TextInput
             .EditType Type:=wdRegularText, Default:=g_value, Format:=""
         End With
      End With
  End With
End If

ActiveDocument.Protect Password:="mypass", NoReset:=False, Type:=wdAllowOnlyFormFields

g_name包含必须插入某些文本的书签名称,

g_value包含必须在bookmark g_name中插入的文本。

此代码正常工作,但前提是g_value长度小于255个字符。如果g_value长度大于255,则宏返回错误"字符串太长"。

我尝试过这样的插入文字:

bm.Range.Select
With Selection
    .Text = g_value
    .Font.Underline = wdUnderlineNone
    .Collapse wdCollapseEnd
End With

这项工作,但文字字段是不可编辑的。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

只需使用文字表格

word_dev_tools

填写此表格

ActiveDocument.Bookmarks(g_name).Range.Fields(1).Result.Text = g_value