簡體   English   中英

VBA:Excel 數據到 MS Word FormField

[英]VBA: Excel Data to MS Word FormField

我正在嘗試將數據從 Excel 文檔復制到 Word 文檔。 我的 excel 文檔有 4 列,我已經將我的 word 文檔設置為有 4 個以 Excel 列名命名的表單域。 當我運行我的代碼時,出現以下錯誤: Run time error '91': Object variable or With block variable not set 我出錯的行用 ** ** 突出顯示。

在我的私人子中,我定義並設置了我的變量wdFormField ,所以我很困惑為什么會出現這個錯誤。 有任何想法嗎?

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

您的代碼中有多個錯誤,包括:

  • X:\abcde.docx\ 不是有效的文件名。 此外,docx 文件並不是真正的模板。
  • 將 wdFormField 定義為 Word.FormField 后,您不能將其設置為 Nothing。

嘗試以下操作:

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