我正在使用.FormFields("WordBookmarkName").Result = stringFromAccess
方法将数据从MS-Access传递到MS-Word文档。
它似乎最多只能传递255个字符。有没有办法可以在MS-Word中将更多内容传递给我的字段?
修改
这是我使用的代码的简化版本,最多可以包含255个字符:
Dim startForms As String
Dim appWord As Word.Application
Dim doc As Word.Document
startForms = String(256, "x")
Set appWord = GetObject(, "Word.Application") 'Set appWord object variable to running instance of Word.
If Err.Number <> 0 Then
Set appWord = New Word.Application 'If Word isn't open, create a new instance of Word.
End If
Set doc = appWord.Documents.Open("C:\myFolder\MyForm.docx", , True)
With doc
.FormFields("wdStartForms").Result = "" 'Clear anything currently in the form's field
.FormFields("wdStartForms").Result = startForms
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
JohnnyBones:这是我在回答之后改编的代码;使用ActiveDocument
无法正常工作,因此我继续使用我所做的doc
引用,在此之后似乎可以使用256个以上的字符:
Dim startForms As String
Dim appWord As Word.Application
Dim doc As Word.Document
startForms = String(256, "x")
Set appWord = GetObject(, "Word.Application") 'Set appWord object variable to running instance of Word.
If Err.Number <> 0 Then
Set appWord = New Word.Application 'If Word isn't open, create a new instance of Word.
End If
Set doc = appWord.Documents.Open("C:\myFolder\MyForm.docx", , True)
With doc
.FormFields("wdStartForms").Result = "" 'Clear anything currently in the form's field
.Bookmarks("wdStartForms").Range.Fields(1).Result.Text = startForms
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
答案 0 :(得分:1)
如果您使用:
Dim FmFld As FormField, Str1 As String
Str1 = (a long string > 256 characters)
Set FmFld = ActiveDocument.FormFields(1)
FmFld.Result = Str1
你得到一个错误:“字符串太长”(一个荒谬的“设计”功能,因为你可以手动完成而没有问题!)。
如果你使用相同:
ActiveDocument.Formfields("Text1").Result = Str1
您可以使用以下方法解决此问题:
ActiveDocument.Unprotect
FmFld.Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
或者,如果您按名称引用表单字段:
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
你也可以尝试传递多个字符串并连接它们,将每个字符串切成小于255个字符的块。