0

我正在使用我在网上找到的代码来填写 Word 文档中的表单域。当我在一个空文档上使用它并向它添加一个表单域时,它可以工作。但是,当我在表单上使用它时,当我执行代码时,我试图填写什么都没有发生。我检查了 Word 中的字段名称,它们与代码匹配,我不知道出了什么问题。
我还检查了 Access 中的数据类型,这不是问题,我现在无法访问代码,但有人知道是什么原因造成的吗?
编辑:这是一个类似的代码,我无法访问完全相同的代码:

Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("C:\Users\" & Environ$("Username") & "\Desktop\Form.doc", , True)
With doc

.FormFields("TextEn").Result = DLookup("[End date]", "[Table1]", "[Table1]![ID Number] =" & [ID2])
.FormFields("TextSt").Result = DLookup("[Starting date]", "[Table1]", "[Table1]![ID Number] =" & [ID2])



.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
4

1 回答 1

0

您可以尝试验证您的 dlookup 是否返回具有以下内容的值:

If DCount("[End date]", "[Table1]", "[Table1]![ID Number] =" & [ID2]) > 0 then
  .FormFields("TextEn").Result = DLookup("[End date]", "[Table1]", "[Table1]![ID Number] =" & [ID2])
End If

在尝试设置字段之前,您必须为每次查找执行此操作。

于 2014-04-11T14:11:40.993 回答