MS Word VBA - 'Normal' 格式未设置并被“标题”格式覆盖

MS Word VBA - 'Normal' formatting not set and overwritten by 'heading'formatting

我正在使用此宏来编辑从 PDF 复制和粘贴的文本,以便将其格式化为填充 word 文档中的整行。

但是,当我粘贴到 header 行上方时:Selection.Style = ActiveDocument.Styles("Normal") 不起作用,文本格式改为 header。

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

非常感谢任何帮助,

谢谢

只有 " " 是 selected。您必须 select 整个段落:

Selection.Expand (wdParagraph)

然后设置样式。

包括要用作 Find/Replace 一部分的样式。 Word 既可以查找格式,也可以在替换过程中应用格式。这使得代码更紧凑,出错的可能性更小(选择可能会改变!)。

  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