2016-04-08 43 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