如何使用excel-vba删除部分以创建Word文档 [英] How to delete a section using excel-vba to create a word document

查看:183
本文介绍了如何使用excel-vba删除部分以创建Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,谢谢你.

我正在使用VBA从Excelfile创建非常复杂的Word文档.应有可能激活某些内容,并将写入单元格中的文本转移到word文档中.我已经完成了.但是,如果未激活,则应删除标签<>",不留任何内容.这意味着它不仅应删除文本,还应删除完整的行".由于行可能是一个部分,因此我不确定仅行是否是此处的正确单词.

I am creating a very complex Word document out of an Excelfile using VBA. It shall be possible to activate something and a text written in a cell shall be transfered to the word document. I got that already done. But if it is not activated the tag "<>" shall be removed, leaving nothing behind. This means that it shall remove not only the text, it shall remove the complete "line". As line might be a section i am not sure if just line is the correct word here.

现在,我使用以下命令找到并替换为":

Right now I find and replace them with "" using:

With WordDoc.Content.Find
   .Execute FindText:=ReplacementTextF, ReplaceWith:="", Replace:=2
End With

但是我也该如何删除行/节?

but how can I delete the line/section, too?

示例:

由于替换是可选的,因此用户可以选择他要在文档中选择的文本.因此,也许不需要ReplacementText4.因此,我需要删除<< ReplacementText4 >>"并删除项目符号.这就是删除行/节的意思.

As the replacement is optional, the user can select which text he wants in the document. Therfore, maybe ReplacementText4 is not needed. So I need to delete the "<< ReplacementText4 >>" and delete the bullet too. This is what I meant by deleting a line/section.

推荐答案

这似乎是有关查找/替换"的问题,但更正确的是有关查找"的问题,然后在找到的文本的位置进行操作.当查找/替换的选项未涵盖替换条件时,这是常见的要求.需求通过以下模式解决.

This appears as a question about Find/Replace but more correctly is a question about Find, then do something at the site of the found text. Its a common requirement when the replacement criteria isn't covered by the options of the find/Replace. The requirement is addressed by the pattern below..

    With <Range>
        With .Find
            <setup find criteria for find>
            .wrap = wdFindStop ' This is essential
        End with
        Do while .Find.Found
            <do your actions here>
            <use .duplicate if you want to do something with the found range
            <e.g. to delete the paragraph with the found text
            .duplicate.paragraphs(1).range.delete
            <move the found range to after the end of the found range>
            .collapse direction:=wdcollapseend
            .moveend unit:=wdCharacter, count:=1
            .find.execute ' must include this to find next instance

        loop

    End with <range>

将此模式转换为代码即可

Translating this pattern into code gives

Sub DeleteParasWithText(this_doc As Word.Document, this_find_text As String)

    With this_doc.content
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .text = this_find_text
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchByte = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute

            End With

            Do While .Find.Found
                ' In the OP case we just want to delete the paragraph
                ' containing the found text
                .Duplicate.Paragraphs(1).Range.Delete
                .Collapse Direction:=wdCollapseEnd
                .Move unit:=wdCharacter, Count:=1
                .Find.Execute
            Loop

        End With
End Sub

在介绍这一点时,我还要感谢@Macropod,因为我从他过去介绍过的许多查找/替换示例中得出了这种模式.

In presenting this I'd also like to acknowledge @Macropod as I derived this pattern from a number of find/replace examples he has presented in the past.

这篇关于如何使用excel-vba删除部分以创建Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆