Microsoft Word宏来修改和粘贴剪贴板文本

时间:2013-04-18 08:07:40

标签: vba ms-word macros word-vba paste

我的目标是:

  1. 将PDF中的文本复制到剪贴板
  2. 在一次移动中(我猜想使用宏),将文本粘贴到MS word中
    • 用空格替换所有换行符
    • 匹配目的地的格式
  3. 我已经完成了创建一个宏,用一个文档或选定部分中的空格替换所有换行符,但不在剪贴板中。目前,宏看起来像这样:

    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
    

    此刻取代整个文字...... 有关如何仅将替换应用于剪贴板然后粘贴此替换的片段的任何帮助将不胜感激!提前谢谢。

2 个答案:

答案 0 :(得分:4)

我建议这样做:

  1. 记住您要粘贴剪贴板数据的当前选择点
  2. 粘贴剪贴板中的内容
  3. 设置粘贴区域的结束点
  4. 仅针对粘贴的文本范围进行替换。
  5. 以下解决方案部分基于问题的代码。评论了必要的(测试)。

    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
    

答案 1 :(得分: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