创建word文档时无法设置字段名
Can't set field name when creating word document
我正在使用 Office 365。我的 Access VBA 代码正在创建 Word 文档。我可以在文档中的 table 上添加一个字段。但是,当我尝试设置字段的名称或字段中的文本时,它被轰炸了。这是我的代码:
Dim WordApp As New Word.Application
Dim WordDoc As New Word.Document
Dim fField As Word.FormField
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
WordApp.Activate
Set WordDoc = WordApp.Documents.Add
WordDoc.Select
' Here I set up some header text, then I set WordRange to be at the end of the document
Set WordTable = WordDoc.Tables.Add(WordRange, 6, 4)
With WordDoc.Tables(1)
Set oRange = .Cell(1, 2).Range
Set fField = ActiveDocument.FormFields.Add(Range:=oRange, Type:=wdFieldFormTextInput)
'
' If I do it either of the two ways below, I get a 'Runtime error 91: Object Variable or With block variable not set'
'
fField.Range.Fields(1).Result.Text = "FirstName"
fField.Range.Text = "FirstName"
'
' The same error comes up when I try to
'
fField.Name = "FirstName"
End With
我在网站上看到了这两种方法的例子,他们似乎都说这会奏效。
请注意,我删掉了一堆也填充 table 的内容,但我认为这对我在这里所做的事情没有任何影响。
它在任一条线上都炸毁了:
fField.Range.Fields(1).Result.Text = "FirstName"
或
fField.Range.Text = "firstName"
(我一次只使用其中一个,我注释掉另一个)。这两个都会导致以下错误。
Runtime error 91: Object Variable or With block variable not set.
在等待响应的过程中,我将所有代码复制到一个临时例程中,并删除了所有无关的东西。我发现当我尝试将字段放入 table 时,它会爆炸(在上面提到的行),但是当我在 table 之外创建字段时,它可以设置文本和字段名没问题!有什么想法可以让它在 table 中工作吗???
我需要先解决 Cell:
.Cell(1, 2).Range.FormFields(1).Result = "Joseph"
和
.Cell(1, 2).Range.FormFields(1).Name = "FirstName"
而不是
fField.Text
.
我正在使用 Office 365。我的 Access VBA 代码正在创建 Word 文档。我可以在文档中的 table 上添加一个字段。但是,当我尝试设置字段的名称或字段中的文本时,它被轰炸了。这是我的代码:
Dim WordApp As New Word.Application
Dim WordDoc As New Word.Document
Dim fField As Word.FormField
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
WordApp.Activate
Set WordDoc = WordApp.Documents.Add
WordDoc.Select
' Here I set up some header text, then I set WordRange to be at the end of the document
Set WordTable = WordDoc.Tables.Add(WordRange, 6, 4)
With WordDoc.Tables(1)
Set oRange = .Cell(1, 2).Range
Set fField = ActiveDocument.FormFields.Add(Range:=oRange, Type:=wdFieldFormTextInput)
'
' If I do it either of the two ways below, I get a 'Runtime error 91: Object Variable or With block variable not set'
'
fField.Range.Fields(1).Result.Text = "FirstName"
fField.Range.Text = "FirstName"
'
' The same error comes up when I try to
'
fField.Name = "FirstName"
End With
我在网站上看到了这两种方法的例子,他们似乎都说这会奏效。
请注意,我删掉了一堆也填充 table 的内容,但我认为这对我在这里所做的事情没有任何影响。
它在任一条线上都炸毁了:
fField.Range.Fields(1).Result.Text = "FirstName"
或
fField.Range.Text = "firstName"
(我一次只使用其中一个,我注释掉另一个)。这两个都会导致以下错误。
Runtime error 91: Object Variable or With block variable not set.
在等待响应的过程中,我将所有代码复制到一个临时例程中,并删除了所有无关的东西。我发现当我尝试将字段放入 table 时,它会爆炸(在上面提到的行),但是当我在 table 之外创建字段时,它可以设置文本和字段名没问题!有什么想法可以让它在 table 中工作吗???
我需要先解决 Cell:
.Cell(1, 2).Range.FormFields(1).Result = "Joseph"
和
.Cell(1, 2).Range.FormFields(1).Name = "FirstName"
而不是
fField.Text
.