2
votes

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?

2
I admit I don't understand a bit- why do you want to replace ^p into " " in clipboard and than paste it to Word. You already managed to do it in document after pasting... Is there something wrong with replacement in Word document? Sorry, please help me to understand. - Kazimierz Jawor
1. I want to replace line breaks by spaces because for every line break in the text pasted from the PDF (automatic line breaks as in "no more space for further words in this line), MS word creates a new paragraph, though it should just continue in the same line if there is still space. --- 2. The current macro replaces pastes the clipboard and then replaces ALL line breaks in the current MS word document with spaces, not only in the text just pasted. --- Does this make more sense? - Bernd
now I've got it... give me a sec to write proposed answer - Kazimierz Jawor

2 Answers

4
votes

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
0
votes

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