【问题标题】:Filling Word forms using Access isn't always working使用 Access 填写 Word 表单并不总是有效
【发布时间】:2014-03-28 12:04:55
【问题描述】:

我正在使用我在网上找到的代码来填写 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

【问题讨论】:

  • Word 有几种不同类型的字段,因此代码很可能在错误的位置查找它们。但是,不看代码就无法判断。可以分享一下吗?
  • 我用代码编辑了帖子
  • On Error Resume Next 将隐藏问题的真实性质并导致事情无声无息地失败。尝试将其注释掉。
  • 我已经编辑了代码,我现在得到的错误是“无效使用 Null”指向带有 .FormFields 的行...
  • 有人知道这个错误吗?

标签: ms-access ms-word vba form-fields


【解决方案1】:

您可以尝试验证您的 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 2013-07-26
    • 2020-04-14
    • 2016-06-12
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多