简体   繁体   中英

MS Word VBA How to Search and copy selected text to beginning of the Paragraph

I want to search and copy year specified in the paragraph and copy it to beginning of the paragraph. following is the code i am working with, it does selects the year but doesn't copy it to the beginning:

Sub CopyYeartoFirst()
'
' Macro1 Macro
'
'    Selection.Find.ClearFormatting
With ActiveDocument.Content
    With Selection.Find
        .Text = "[0-9]{4}"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
        .Execute
    End With
While .Find.Found = True

    Selection.Copy
    Selection.HomeKey Unit:=wdLine
    Selection.PasteAndFormat (wdPasteDefault)
    'Selection.TypeText Text:=" -- "
    .Find.Execute

    'Selection.Find.ClearFormatting
Wend


End With
End Sub

You will need to use something like this before pasting your search results

Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
oRng.Collapse wdCollapseStart
oRng.Select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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