How to delete a section using excel-vba to create a word document

流过昼夜 提交于 2020-01-23 13:07:07

问题


Hello and thank you in advance.

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?

EDIT1:

Example:

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/53317548/how-to-delete-a-section-using-excel-vba-to-create-a-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!