将MS-Access string> 255个字符传递到MS-Word字段

问题描述:

我使用.FormFields("WordBookmarkName").Result = stringFromAccess方法将数据从MS-Access传递到MS-Word文档。将MS-Access string> 255个字符传递到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 
+1

索引通常为1:通过FormFields,名称/书签通常“覆盖”FormField,而不是其他任何东西。因此它的范围“覆盖”1个字段,索引是范围中的“第n个”字段,而不是整个文档。从理论上讲,书签可以被移动(但是可以在表单中创建一个“错误状态”),理论上AFAICR可以将字段嵌套在FormField中,这可能会改变索引。但我几乎没有见过它。 – 2014-11-24 10:54:02

+0

ahh ok,很高兴知道:)'.Fields(1)'当我的字段有255个字符时填充,但不填充256个字符。我一直在测试'String(255,“x”)'字符串(256,“x”)'。 – 2014-11-24 11:23:49

+0

实际上,它没有填充,我刚刚评论了错误位 – 2014-11-24 11:40:10

如果你使用:

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 

或者如果你指的是按名称formfield:

ActiveDocument.Unprotect 
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = Str1 
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True 

您也可以尝试传递多个字符串并将它们连接起来,将每个字符串切分成少于255个字符的块。

+0

谢谢约翰尼。我无法得到这个工作,虽然 - 请参阅我原来的问题编辑。 – 2014-11-24 10:13:25

+0

没关系,我设法通过参考我制作的'.doc'参考而不是主动文档:) – 2014-11-24 11:56:46

+4

顺便说一句,除非你是Dave Rado,否则你至少应该相信这个解决方案的来源,例如http:///word.mvps.org/FAQs/MacrosVBA/SetLongFmFldResult.htm。 – 2014-11-24 15:50:22