简体   繁体   English

VBA:Excel 数据到 MS Word FormField

[英]VBA: Excel Data to MS Word FormField

I'm trying to copy data from an Excel document to a Word document.我正在尝试将数据从 Excel 文档复制到 Word 文档。 My excel doc has 4 columns and I've set my word document to have 4 formfields named after the Excel column names.我的 excel 文档有 4 列,我已经将我的 word 文档设置为有 4 个以 Excel 列名命名的表单域。 When I run my code I get the following error: Run time error '91': Object variable or With block variable not set .当我运行我的代码时,出现以下错误: Run time error '91': Object variable or With block variable not set The line where I'm erroring out is highlighted with ** **.我出错的行用 ** ** 突出显示。

In my private sub I defined and set my variable wdFormField so I'm confused why I'm getting this error.在我的私人子中,我定义并设置了我的变量wdFormField ,所以我很困惑为什么会出现这个错误。 Any ideas?有任何想法吗?

Option Explicit


Sub Checklist()
Dim WDApp As Word.Application
Dim myDoc As Word.Document
Dim mywdRange As Word.Range
Dim r As Long
Dim m As Long
Dim dDate As Date
Dim strNumber As String

Set WDApp = New Word.Application
With WDApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With

With Sheets("Sheet1")
m = .Range("A" & .Rows.Count).End(xlUp).Row
End With

For r = 3 To m
If Range("A" & r).EntireRow.Hidden = False Then

Set myDoc = WDApp.Documents.Add(Template:="X:\abcde.docx\")
Copy_Cell_To_Form_Field myDoc, Range("A" & r).Value, "Text1"
Copy_Cell_To_Form_Field myDoc, Range("B" & r).Value, "Text2"
Copy_Cell_To_Form_Field myDoc, Range("C" & r).Value, "Text3"
Copy_Cell_To_Form_Field myDoc, Range("D" & r).Value, "Text4"

myDoc.SaveAs2 Filename:="X:\abcde.docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
myDoc.Close
End If
Next r

End Sub
Private Sub Copy_Cell_To_Form_Field(doc As Word.Document, cellValue As Variant, formFieldName As String)

    Dim i As Integer
    Dim wdFormField As Word.FormField

    
    Set wdFormField = Nothing
    i = 1
    ' the next line gives me: Run time error '91': Object variable or With block variable not set
    While i <= doc.FormFields.Count And wdFormField Is Nothing
        If doc.FormFields(i).Name = formFieldName Then Set wdFormField = doc.FormFields(i)
        i = i + 1
    Wend
    
    If Not wdFormField Is Nothing Then
        wdFormField.Result = cellValue
    Else
        MsgBox "Form field bookmark " & formFieldName & " doesn't exist in " & doc.Name
    End If
    
End Sub

There are multiple errors in your code, including:您的代码中有多个错误,包括:

  • X:\abcde.docx\ is not a valid filename. X:\abcde.docx\ 不是有效的文件名。 Moreover, a docx file isn't really a template.此外,docx 文件并不是真正的模板。
  • Having defined wdFormField As Word.FormField you can't set it to Nothing.将 wdFormField 定义为 Word.FormField 后,您不能将其设置为 Nothing。

Try the following:尝试以下操作:

Sub Checklist()
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdRng As Word.Range
Dim xlSht As Worksheet, r As Long, c As Long
Set xlSht = Sheets("Sheet1")
With wdApp
  .Visible = True
  For r = 3 To xlSht.Range("A" & xlSht.Rows.Count).End(xlUp).Row
    If xlSht.Range("A" & r).EntireRow.Hidden = False Then
      Set wdDoc = wdApp.Documents.Add(Template:="X:\abcde.docx")
      With wdDoc
        For c = 1 To 4
          If .Bookmarks.Exists("Text" & c) Then
            .Bookmarks("Text" & c).Range.Fields(1).Result.Text = xlSht.Cells(r, c).Text
          Else
            MsgBox "Form field bookmark 'Text" & c & "' doesn't exist in:" & vbCr & _
              .AttachedTemplate.FullName
          End If
        Next
        .SaveAs2 Filename:="X:\abcde.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        .Close
      End With
    End If
  Next r
  .Quit
End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM