2016-04-08 51 views
0

我正在使用此宏編輯從PDF複製和粘貼的文本,以便將其格式化爲填充word文檔中的整行。MS Word VBA - '正常'格式化未設置並被「標題」格式覆蓋

但是,當我粘貼標題上面的行時:Selection.Style = ActiveDocument.Styles("Normal") 不起作用,而文本被格式化爲標題。

Sub Clean_PDF_Para() 
'crude macro to fix paragraph markers (invisible)(so text copied from pdf is formatted to fill lines 
'currently based on selected range 


    With Selection.Find 
     .Text = "^p" 
     .Replacement.Text = " " 
     .Wrap = wdFindStop ' think this is required to stop it fixing (breaking) the whole selction 
    End With 

    Selection.Find.Execute Replace:=wdReplaceAll 
    Selection.Style = ActiveDocument.Styles("Normal") 'added to fix the paragraph style so it doesn't take the form of a heading. 
End Sub 

任何幫助非常讚賞,

感謝

回答

1

只有" "選擇。您必須選擇全段:

Selection.Expand (wdParagraph) 

然後設置樣式。

1

包含您想要用作查找/替換的一部分的樣式。 Word可以找到格式,並將格式應用爲替換過程的一部分。這使得compacter的代碼和錯誤的機會減少(選擇可能會改變!)。

With Selection.Find 
     .Text = "^p" 
     .Replacement.Text = " " 
     .Replacement.Style = wdStyleNormal 
     .wrap = wdFindStop ' think this is required to stop it fixing (breaking) the whole selction 
     .Execute Replace:=wdReplaceAll 
    End With