2016-06-28 108 views
0

我想将复制的文本粘贴到.Replacement.Text中。我用Selection Paste尝试了这个,但这不起作用。Microsoft Word宏复制文本并在查找和替换过程中使用它

需要明确的是:我想要一个Microsoft Word文档中复制文本,我要复制的文字变成了乘法fieldcodes ALT + F9 。所以我的Word文档会一次更改所有的域代码。

Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend 
    Selection.Copy 
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes 
    Selection.WholeStory 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "00000" 
     .Replacement.Text ="Selection.Paste" 
     .Forward = True 
     .Wrap = wdFindAsk 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes 
    Selection.WholeStory 
    Selection.Fields.Update 
End Sub 
+0

有VBA中没有直接的剪贴板支持。您可以使用Windows API功能的剪贴板(一个简单的Google搜索,你会发现一个代码示例如何做到这一点)。但是,在投资这个选项之前:将会使用当前选定的文本作为您的合适选项?访问当前选择要简单得多(只需使用'.Replacement.Text = Selection.Text') –

回答

0

没有必要使用Selection.WholeStory

Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend 
     Selection.Copy 
     ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes 
'  Selection.WholeStory 
    searchText=selection.text 
     Selection.Find.ClearFormatting 
     Selection.Find.Replacement.ClearFormatting 
     With Selection.Find 
      .Text = "00000" 
      .Replacement.Text =searchText 
      .Forward = True 
      .Wrap = wdFindAsk 
      .Format = False 
      .MatchCase = False 
      .MatchWholeWord = False 
      .MatchWildcards = False 
      .MatchSoundsLike = False 
      .MatchAllWordForms = False 
     End With 
     Selection.Find.Execute Replace:=wdReplaceAll 
     ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes 
     Selection.WholeStory 
     Selection.Fields.Update 
    End Sub 
相关问题