如何使用VBA从Word文档中查找和复制特定内容并将其粘贴到另一个文档中

时间:2018-08-03 16:47:57

标签: vba word-vba

我已经录制了一个宏,可以从一个单词文档中提取特定内容到另一个单词文档,但是它的功能有限。我需要将所有选定的内容提取到新文档中,并用后缀保存或在同一文件名中添加单词“ glos”。

由于文件名处于活动状态,没有将宏用于其他文件,即使更改了宏中的文件名,它也会提取内容,直到记录的宏数为止。

使用正则表达式"\<(MN_GLOS)\>(*)\</(\1)\>"查找指定的内容。

输入:

    <CHAP_NUM>01</CHAP_NUM>
    <CHAP_TL> Lorem Ipsum</CHAP_TL>

    <PARA>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</PARA>
    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>

    <PARA>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</PARA>

    <PARA>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</PARA>

    <PARA>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</PARA>

    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>

    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>

    <PARA>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</PARA>

    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>

    Sub Macro5()
    '
    ' Macro5 Macro
    '
    '
        selection.Find.ClearFormatting
        With selection.Find
            .Text = "\<(MN_GLOS)\>(*)\</(\1)\>"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
        selection.Find.Execute
        selection.Copy
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
        selection.TypeParagraph
        Windows("ca_ch01__studyingsexlty_24feb2018CE_QA.doc [Compatibility Mode]") _
            .Activate
        Application.Browser.Next
        selection.Copy
        Windows("Doc1.docx").Activate
        selection.PasteAndFormat (wdUseDestinationStylesRecovery)
    End Sub

输出(新docx)

    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>
    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>
    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>
    <MN_GLOS>
    Lorem Ipsum is simply dummy text.
    Lorem Ipsum is simply dummy text.
    </MN_GLOS>

请帮助我将内容提取到新文档中。

0 个答案:

没有答案