当字段为空时删除行

我正在尝试使用Visual Basic,以便可以使用Excel中的数据填充Word模板。 我有一个macros从Microsoft Excel中的一个表填充Word文档表中的字段。 到目前为止,如果Excel表小于单词表,则在单词表中打印“错误!没有文档variables”消息,并使用macros(下面)删除该字段。 但是,我也想删除发生此错误的Word表中的整个行。 你能帮我弄清楚如何做到这一点?

Sub Rectangle_Click() Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Dim ws As Worksheet Dim oHeader As Word.HeaderFooter Dim oSection As Word.Section Dim oFld As Word.Field Dim flds As Word.Fields Dim fld As Word.Field Set ws = ThisWorkbook.Sheets("Sheet1") ws.Activate On Error Resume Next Set wrdApp = GetObject(, "Word.Application") If wrdApp Is Nothing Then Set wrdApp = CreateObject("Word.Application") On Error GoTo 0 wrdApp.Visible = True Set wrdDoc = wrdApp.Documents.Add(Template:="C:\Documents\mytemplate.dotm") With wrdDoc .Variables("foo1").Value = Range("A5").Value .Variables("foo2").Value = Range("A6").Value .Variables("foo3").Value = Range("A7").Value .Variables("bar1").Value = Range("B5").Value .Variables("bar2").Value = Range("B6").Value .Variables("bar3").Value = Range("B7").Value .Range.Fields.Update End With wrdDoc.Range.Fields.Update Set flds = ActiveDocument.Fields For Each fld In flds If fld.Type = wdFieldDocVariable Then If fld.Result = "Error! No document variable supplied." Then Debug.Print fld.Code 'ALSO DELETE THE ROW WHERE THIS EMPTY FIELD WAS FOUND!!' fld.Delete End If End If Next Set wrdDoc = Nothing Set wrdApp = Nothing Application.CutCopyMode = False End Sub 

我怎样才能摆脱“没有文档variables提供”发生的行(或单元格)?

如果您想要删除当前所选内容的整个表格行,请使用:

 Word.Selection.Rows.Delete