简体   繁体   English

将一种样式的最后一段转换为另一种样式,ms字,VBA

[英]Turn the last paragraph of one style into another style, ms-word, VBA

This code allows to change all text with style "myStyleOne" for "myStyleTwo". 此代码允许将样式为“ myStyleOne”的所有文本更改为“ myStyleTwo”。

Option Explicit
Sub replaceStyleForAnotherStyle()
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("myStyleOne")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("myStyleTwo")
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

What should the code look like if I want to turn only the last paragraph of "myStyleOne" into "myStyleTwo"? 如果我只想将“ myStyleOne” 的最后一段变成“ myStyleTwo”,代码应该是什么样?

To find the last paragraph with a given style: 要查找具有给定样式的最后一段:

Function lastOfStyle(st As String) As Paragraph
    For i = ActiveDocument.Paragraphs.Count To 1 Step -1
        If ActiveDocument.Paragraphs(i).Style = st Then
            Set lastOfStyle = ActiveDocument.Paragraphs(i)
            Exit Function
        End If
    Next
End Function

Now you can change the style with this simple statement: 现在,您可以使用以下简单语句更改样式:

lastOfStyle("MyStyle1").Style = "MyStyle2"

But if you want to do it in your macro, for other reasons that I missed, then you should add the following at the beginning of your macro: 但是,如果您想在宏中执行此操作,由于我错过的其他原因,则应在宏的开头添加以下内容:

Dim p as Paragraph: Set p = lastOfStyle("Normal")
if p Is Nothing then Exit Sub
p.Range.Select

Then set .Wrap = wdFindStop in the With Selection.Find bloc instead of * .Wrap = wdFindContinue * 然后在With Selection.Find设置.Wrap = wdFindStop块而不是* .Wrap = wdFindContinue *

Have you seen this answer? 你看到这个答案了吗? It really helps: answer . 它确实有帮助: answer The things you might know: in VBA there are subroutines (sub) and functions. 您可能知道的事情:在VBA中,有子例程(sub)和函数。 Functions can be used in many subroutines and should go separately. 函数可以在许多子例程中使用,应该分开使用。

Great. 大。 You want to copy and use the code? 您要复制并使用代码吗? Here you are, just copy it all to the developer tab on the ribbon: http://codepad.org/Wd5Rer4y . 在这里,只需将它们全部复制到功能区上的开发人员选项卡上: http : //codepad.org/Wd5Rer4y Then you can run sub replaceStyleForAnotherStyleSimple() or sub replaceStyleForAnotherStyleComplicated() . 然后,您可以运行sub replaceStyleForAnotherStyleSimple()sub replaceStyleForAnotherStyleComplicated() Both of them rely on function lastOfStyle . 它们都依赖函数 lastOfStyle Click alt+f8 and choose wisely. 单击alt + f8并明智地选择。

Great. 大。 But is it possible to do the same thing without function? 但是,没有功能,是否有可能做同样的事情? Sure! 当然! Here it comes! 它来了! replaceStyleForAnotherStyleNoFunction()

Sub replaceStyleForAnotherStyleNoFunction()
    Dim parCountDown, i, sMyPar
    parCountDown = ActiveDocument.Paragraphs.Count
    For i = parCountDown To 1 Step -1
        If ActiveDocument.Paragraphs(i).Style = "myStyleOne" Then 'change the part for: "Normal"
            ActiveDocument.Paragraphs(i).Style = "myStyleTwo" 'change the part for: "Heading 1"
            'sMyPar = ActiveDocument.Paragraphs(i).Range.Text
            'MsgBox (sMyPar) 'message box with that paragraph
            Exit Sub
        End If
    Next
End Sub

So you see here three ways to do the same thing and easy path to copy all that. 因此,您在这里看到了三种执行相同操作的方法,以及复制所有内容的简便方法。 Stay tuned and Good luck! 敬请期待,祝你好运!

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

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