简体   繁体   中英

Modify and paste clipboard text

My goal is to:

  1. Copy text in a PDF to the clipboard
  2. In a single move, paste the text to MS Word while
    • Replacing all line breaks with a space
    • Matching the destination's formatting

I created a macro which replaces all line breaks with spaces in a document.

Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "^p"
    .Replacement.Text = " "
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

How to apply the replacement to the clipboard and then paste this replaced snippet?

I propose to do it this way:

  1. remember current selection point where you would paste your clipboard data
  2. paste what you have in clipboard
  3. set ending point of pasted area
  4. do replacement only for just pasted range of text.

The following solution based partially on the code from the question. What was necessary (for test) was commented.

Sub replacement_for_selection()

'Selection.Find.Execute Replace:=wdReplaceAll
'Selection.WholeStory

Dim rngFrom, rngTo
    rngFrom = Selection.Start
Selection.PasteAndFormat (wdFormatOriginalFormatting)
    rngTo = Selection.End
ActiveDocument.Range(rngFrom, rngTo).Select

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "^p"
    .Replacement.Text = " "
    .Forward = False    '!!!
    .Wrap = wdFindStop   '!!!
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute , , , , , , , , , , wdReplaceAll
End Sub

It worked for me too, but I improved it, adding a command to eliminate double spaces:

Sub KM()
'
' KM Macro
' Paste and eliminate line breaks and double spaces.
'
'Selection.Find.Execute Replace:=wdReplaceAll
'Selection.WholeStory

Dim rngFrom, rngTo
    rngFrom = Selection.Start
Selection.PasteAndFormat (wdFormatOriginalFormatting)
    rngTo = Selection.End
ActiveDocument.Range(rngFrom, rngTo).Select

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "^p"
    .Replacement.Text = " "
    .Forward = False    '!!!
    .Wrap = wdFindStop   '!!!
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute , , , , , , , , , , wdReplaceAll

Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "  "
        .Replacement.Text = " "
        .Forward = False
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

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