字段为空时删除行
Delete row when fields are empty
我正在尝试使用 Visual Basic,以便可以使用来自 excel 的数据填充 Word 模板。我有一个宏,它从 Microsoft Excel 中的 table 中填充 Word 文档 table 中的字段。到目前为止,如果 excel table 小于单词 table,消息 "Error! No document variable supplied" 打印在单词 table 中,我使用宏删除该字段(以下)。但是,我还想删除发生此错误的 Word table 中的整行。你能帮我弄清楚怎么做吗?
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
如何删除出现 "no document variable supplied" 的行(或单元格)?
如果要删除当前选择所在的 table 的整行,请使用:
Word.Selection.Rows.Delete
我正在尝试使用 Visual Basic,以便可以使用来自 excel 的数据填充 Word 模板。我有一个宏,它从 Microsoft Excel 中的 table 中填充 Word 文档 table 中的字段。到目前为止,如果 excel table 小于单词 table,消息 "Error! No document variable supplied" 打印在单词 table 中,我使用宏删除该字段(以下)。但是,我还想删除发生此错误的 Word table 中的整行。你能帮我弄清楚怎么做吗?
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
如何删除出现 "no document variable supplied" 的行(或单元格)?
如果要删除当前选择所在的 table 的整行,请使用:
Word.Selection.Rows.Delete