简体   繁体   English

VBA Word-更改标题会使样式变松

[英]VBA Word - changing the heading makes it loosing its style

There's a bunch of questions here asking how to get all headings, but apparently nobody tried to change a heading ... 这里有很多问题询问如何获取所有标题,但显然没有人尝试更改标题...

I want to change some second level headings from Old Heading to New Heading , so I did this ... 我想将一些二级标题从“ 旧标题”更改为“ 新标题” ,所以我这样做了...

Sub changeHeading()
    Dim p As Paragraph
    For Each p In ActiveDocument.Paragraphs
        If p.Style = "Heading 2" Then _
            If p.Range.Text = "Old Heading" & vbCr Then p.Range.Text = "New Heading" & vbCr
    Next p
End Sub

The headings get the new name, but they also get a different style - they get the style of the following paragraph - ie if the text after the heading had style table text the heading will have style table text afterwards (the font size will remain the one from the old heading), the numbering of the chapters is lost. 标题获得了新名称,但它们也获得了不同的样式-它们获得了以下段落的样式-即,如果标题之后的文本具有样式表文本,则标题随后将具有样式表文本 (字体大小将保持不变)一个旧标题),则章节编号丢失。 If I set the style back p.Style = "Heading 2" this will make the first text after that heading (ie the next paragraph) having the Heading 2 style, though it's still the same paragraph p (with changed text, but same position in the document). 如果我将样式设置为p.Style = "Heading 2"这将使该标题(即下一个段落)之后的第一个文本具有Heading 2样式,尽管它仍是同一段落p (具有更改的文本,但位置相同)在文档中)。

Situation before: 之前的情况:

1.4 Old Heading
some text

after changing the heading and setting back the style 更改标题并调回样式后

New Heading
1.4 some text    

Where some text has the style Heading 2 and New Heading has font size of the heading, but no numbering. 其中some text具有Heading 2的样式,而New Heading具有New Heading字体大小,但没有编号。

Second problem, it takes unbelievable long (some seconds for a small document with just few paragraphs). 第二个问题,它花费了令人难以置信的长时间(对于只有几段的小文档来说,要花几秒钟)。

So, how to change a heading? 那么,如何更改标题? And how to do it in a way that won't take minutes on a big document? 以及如何以无需花费大量时间处理大型文档的方式进行操作? Thanks for any help. 谢谢你的帮助。

EDIT: 编辑:

I found that changing the text with 我发现用

Call p.Range.Find.Execute(FindText:="Old Heading", ReplaceWith:="New Heading")

will keep the style, so this will solve my original problem of changing the text, but I'd still like to know why setting the paragraphs text makes it loosing its style. 会保留样式,因此可以解决我原来更改文本的问题,但是我仍然想知道为什么设置段落文本会使样式变松。

The reason the style is changing is because you're overwriting the paragraph mark (vbCr). 样式更改的原因是因为您覆盖了段落标记(vbCr)。 The paragraph-level formatting is associated directly with the paragraph character. 段落级格式直接与段落字符关联。 When you delete it, then insert another at the start of the next paragraph, the new paragraph will adopt the formatting of the paragraph in which it originated. 当您删除它时,然后在下一个段落的开头插入另一个,新的段落将采用其原始段落的格式。

And incidentally, using Range.Find with Replace would probably be much more efficient than looping the paragraphs collection. 顺便说一句,使用Range.Find和Replace可能比循环段落集合更有效。 Word's Find functionality can also search for formatting. Word的“查找”功能还可以搜索格式。 So you can use a single Find action to locate both the Heading 2 style AND the specific text (without the paragraph mark). 因此,您可以使用单个“查找”操作来查找“标题2”样式和特定文本(不带段落标记)。 And then use Replace to write the new text. 然后使用“替换”编写新文本。

Test in the Word UI, then record a macro to get the basic syntax. 在Word UI中进行测试,然后记录一个宏以获取基本语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM