简体   繁体   English

如何使用MailMerge字段作为删除Word中的一行表的条件? 需要VBA代码

[英]How to use MailMerge field as a condition for deleting a row of table in Word? VBA code needed

Creating a bill/letter: 创建账单/信件:

This is a Excel-to-Word-bill-creation attempt to automatize work process. 这是一种Excel-to-Word-bill-creation尝试,可以自动化工作流程。

I have used MailMerge to add fields from Excel to Word tables. 我使用MailMerge将字段从Excel添加到Word表格。 I have 3 separate tables in Word, each with different number of rows. 我在Word中有3个单独的表,每个表都有不同的行数。 In rows 1 to n-1, there are 4 columns, and in the nth row there are 2 columns. 在行1到n-1中,有4列,在第n行中有2列。 I would like to use macro to delete: 我想用宏来删除:

  1. whole table, if a cell(n,2) value is 0 ("n" - last row of the table, "0" - MailMerge field of value "0.00"). 整个表,如果单元格(n,2)的值为0(“n” - 表的最后一行,“0” - MailMerge字段的值为“0.00”)。
  2. a row, if a cell(i,4) value is 0 (1 < "i" < n, "0" - MailMerge field of value "0.00"). 行,如果单元格(i,4)的值为0(1 <“i”<n,“0” - MailMerge字段的值为“0.00”)。
    Sub DeleteEmptyTablerowsandcolumns()
        Application.ScreenUpdating = False
        Dim Tbl As Table, cel As Cell, i As Long, n As Long
        With ActiveDocument
            For Each Tbl In .Tables
                n = Tbl.Rows.Count
                m = Tbl.Range.Rows(n).Cells.Count
                If (Tbl.Cell(n, 2).Range.Text = "0.00") Then
                    Tbl.Delete
                    Selection.TypeBackspace
                Else: For i = n - 1 To 2 Step -1
                        If Len(Tbl.Cell(i, 4).Range.Text) <= 2 Then
                            Tbl.Rows(i).Delete
                        End If

                      Next i
                End If
            Next Tbl
        End With
        Application.ScreenUpdating = True
        End Sub

Right now, I cannot target cell values with MailMerge fields, for the code to work. 现在,我无法使用MailMerge字段定位单元格值,以使代码生效。 The whole code would be a great thing to get. 整个代码将是一件很棒的事情。 I assume solution is trivial, but still out of the reach. 我认为解决方案是微不足道的,但仍然无法实现。 Thanks in advance! 提前致谢!

You infer that you're using mailmerge but, if that is so, there will be no mergefields in the output once the merge has completed. 您推断您正在使用mailmerge但是,如果是这样,则在合并完成后输出中将没有合并域。 The fact you're still finding mergefields therefore suggests you're at most doing a mailmerge preview. 因此,您仍然可以找到合并域,这表明您最多只能进行mailmerge预览。 Try the following, which completes the merge then does the necessary processing. 尝试以下操作,完成合并然后进行必要的处理。 You can either run the code manually as you've apparently been doing, or by choosing Finish and Merge>Edit Individual Documents. 您可以像显而易见的那样手动运行代码,也可以选择“完成”和“合并”>“编辑单个文档”。

Sub MailMergeToDoc()
Application.ScreenUpdating = False
Dim Tbl As Table, r As Long
ActiveDocument.MailMerge.Execute
For Each Tbl In ActiveDocument.Tables
  With Tbl
    If Split(.Range.Cells(.Range.Cells.Count).Range, vbCr)(0) = "0.00" Then
      .Delete
    Else
      For r = .Rows.Count - 1 To 2 Step -1
        If Split(.Cell(r, 4).Range.Text, vbCr)(0) = "0.00" Then .Rows(r).Delete
      Next r
    End If
  End With
Next Tbl
Application.ScreenUpdating = True
End Sub

As coded, the macro assumes the cells to be tested contain "0.00"; 如编码,宏假定要测试的单元格包含“0.00”; if that's not what they contain post-merge, edit the code accordingly. 如果这不是它们在合并后包含的内容,则相应地编辑代码。

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

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