2

我的目标是:

  1. 将 PDF 中的文本复制到剪贴板
  2. 在一个移动中,将文本粘贴到 MS Word,同时
    • 用空格替换所有换行符
    • 匹配目的地的格式

我创建了一个宏,它用文档中的空格替换所有换行符。

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

如何将替换应用到剪贴板,然后粘贴这个替换的片段?

4

2 回答 2

4

我建议这样做:

  1. 记住您将粘贴剪贴板数据的当前选择点
  2. 粘贴剪贴板中的内容
  3. 设置粘贴区域的结束点
  4. 仅对刚刚粘贴的文本范围进行替换。

以下解决方案部分基于问题中的代码。评论了必要的(测试)。

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
于 2013-04-18T09:03:08.217 回答
0

它也对我有用,但我改进了它,添加了一个命令来消除双空格:

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
于 2013-10-27T21:47:28.617 回答