2013-04-18 134 views
2

我的目標是:微軟Word宏修改和粘貼剪貼板文本

  1. 在PDF到剪貼板
  2. 在單招(使用宏,我想)複製文本,粘貼文本到MS字,而
    • 更換所有換行符以空格
    • 匹配目標的格式

我已經完成了創建一個宏,它將整個文檔中或選定部分中的空格替換爲所有換行符,但不是在剪貼板中。目前,宏看起來是這樣的:

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 

這在目前替代整個文字... 如何只有更換申請到剪貼板,然後粘貼更換片斷會大爲任何幫助不勝感激!提前致謝。

+0

我承認你爲什麼要替換'我不明白,一個比特^在剪貼板中輸入''「',然後粘貼到Word中。您已經設法在粘貼後在文檔中執行它...在Word文檔中更換有什麼問題嗎?對不起,請幫我理解。 –

+0

1.我想用空格替換換行符,因爲對於從PDF粘貼的文本中的每一行換行符(自動換行符如「此行中更多單詞不再有空格)」,MS字創建一個新段落,儘管它 --- 2.當前宏替換粘貼剪貼板,然後用空格替換當前MS word文檔中的所有換行符,而不僅僅是剛剛粘貼的文本。 --- 這是否更有意義? – Bernd

+0

現在我已經得到了...給我一秒鐘來寫建議的答案 –

回答

4

我建議做這種方式:

  1. 記住當前選擇的地步,你會粘貼剪貼板數據
  2. 內容粘貼剪貼板中有粘貼區域
  3. 設定終點只做粘貼範圍的文本替換。

以下解決方案部分基於問題的代碼。有必要(測試)被評論。

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

真棒,爲我工作,非常感謝! – Bernd

+0

高興地知道,不客氣: ) –

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