问题描述
我正在使用.FormFields("WordBookmarkName").Result = stringFromAccess
方法将数据从MS-Access传递到MS-Word文档.
I'm using .FormFields("WordBookmarkName").Result = stringFromAccess
method to pass data out of MS-Access to an MS-Word document.
似乎最多只能传递255个字符.有什么方法可以使我在MS-Word中获得更多的收获?
It seems it can only pass up to 255 characters. Is there a way I can pass more to my field in 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个以上的字符:
JohnnyBones: this is the code I adapted after your answer; using ActiveDocument
wasn't working, so I continued to use the doc
reference I'd made and it seemed to work ok with 256+ characters after that:
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
推荐答案
如果使用:
Dim FmFld As FormField, Str1 As String
Str1 = (a long string > 256 characters)
Set FmFld = ActiveDocument.FormFields(1)
FmFld.Result = Str1
您会收到一个错误:字符串太长"(这是一个荒谬的设计"功能,因为您可以手动完成而不会出现问题!).
You get an error: "String too long" (a ridiculous "design" feature, given that you can do it manually without problems!).
与使用相同:
ActiveDocument.Formfields("Text1").Result = Str1
您可以使用以下方法解决此问题:
You can get round this by using:
ActiveDocument.Unprotect
FmFld.Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
或者如果您通过名称引用表单域:
Or if you're referring to the formfield by name:
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
您还可以尝试传递多个字符串并将其串联,将每个字符串切成少于255个字符的块.
You could also try passing multiple strings and concatenating them, chopping each string into chunks less than 255 characters.
这篇关于将MS-Access字符串> 255个字符传递给MS-Word字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!