段落样式复制/重命名

huangapple go评论69阅读模式
英文:

Paragraph style copy/renaming

问题

我有一堆文档,它们的格式是X标题Y(BT标题1,BT标题2,BT标题3),而不是标题1,标题2,标题3)... 所以它们不会显示在交叉引用/标题中。

有没有办法编写VBA来重命名它们,或者从X标题Y复制格式到标题Y?

非常感谢您提前的帮助!

在网上搜索但找不到相关文章。

英文:

I have a bunch of document that has X Heading Y (BT Heading 1, BT Heading 2, BT Heading 3) format instead of Heading 1, Heading 2, Heading 3)... so they won't show up in cross reference / heading.

Is there a way to write a VBA to rename these, or to copy the format from X Heading Y to Heading Y ?

Thanks so much in advance!

Searched online but couldn't find any related articles.

段落样式复制/重命名

答案1

得分: 0

Sub Paragraph_style_copy_renaming()
    ' 代码部分不要翻译
End Sub

Sub UpdateStyle2RangeStyle(sourceStyle As Style, destStyle As Style)
    ' 代码部分不要翻译
End Sub
英文:

Timothy Rylatt is right, you can refer to his point and implement it, the following code I implemented is a similar one, you can try first:

Sub Paragraph_style_copy_renaming()
    Dim destStyleName, sourceStyleName, d As Word.Document, e, i
    destStyleName = Array("BT Heading 1", "BT Heading 2", "BT Heading 3")
    sourceStyleName = Array(WdBuiltinStyle.wdStyleHeading1, WdBuiltinStyle.wdStyleHeading2, WdBuiltinStyle.wdStyleHeading3)
    Set d = ActiveDocument
    For Each e In sourceStyleName
        UpdateStyle2RangeStyle d.Styles(e), d.Styles(destStyleName(i))
        i = i + 1
    Next e
    i = 0
    With d.Range.Find
        .ClearAllFuzzyOptions
        .ClearFormatting
        For Each e In destStyleName
            .Style = d.Styles(e)
            .Wrap = wdFindContinue
            .Replacement.Style = sourceStyleName(i)
            .Execute Replace:=wdReplaceAll
            i = i + 1
        Next e
    End With
End Sub


Sub UpdateStyle2RangeStyle(sourceStyle As Style, destStyle As Style)
    sourceStyle.AutomaticallyUpdate = destStyle.AutomaticallyUpdate
    With sourceStyle.Font
        .NameFarEast = destStyle.Font.NameFarEast
        .NameAscii = destStyle.Font.NameAscii
        .NameOther = destStyle.Font.NameOther
        .Name = destStyle.Font.Name
        .Size = destStyle.Font.Size
        .Bold = destStyle.Font.Bold
        .Italic = destStyle.Font.Italic
        .Underline = destStyle.Font.Underline
        .UnderlineColor = destStyle.Font.UnderlineColor
        .StrikeThrough = destStyle.Font.StrikeThrough
        .DoubleStrikeThrough = destStyle.Font.DoubleStrikeThrough
        .Outline = destStyle.Font.Outline
        .Emboss = destStyle.Font.Emboss
        .Shadow = destStyle.Font.Shadow
        .Hidden = destStyle.Font.Hidden
        .SmallCaps = destStyle.Font.SmallCaps
        .AllCaps = destStyle.Font.AllCaps
        .Color = destStyle.Font.Color
        .Engrave = destStyle.Font.Engrave
        .Superscript = destStyle.Font.Superscript
        .Subscript = destStyle.Font.Subscript
        .Scaling = destStyle.Font.Scaling
        .Kerning = destStyle.Font.Kerning
        .Animation = destStyle.Font.Animation
        .DisableCharacterSpaceGrid = destStyle.Font.DisableCharacterSpaceGrid
        .EmphasisMark = destStyle.Font.EmphasisMark
        .Ligatures = destStyle.Font.Ligatures
        .NumberSpacing = destStyle.Font.NumberSpacing
        .NumberForm = destStyle.Font.NumberForm
        .StylisticSet = destStyle.Font.StylisticSet
        .ContextualAlternates = destStyle.Font.ContextualAlternates
    End With
    With sourceStyle.ParagraphFormat
        .LeftIndent = destStyle.ParagraphFormat.LeftIndent
        .RightIndent = destStyle.ParagraphFormat.RightIndent
        .SpaceBefore = destStyle.ParagraphFormat.SpaceBefore
        .SpaceBeforeAuto = destStyle.ParagraphFormat.SpaceBeforeAuto
        .SpaceAfter = destStyle.ParagraphFormat.SpaceAfter
        .SpaceAfterAuto = destStyle.ParagraphFormat.SpaceAfterAuto
        .LineSpacingRule = destStyle.ParagraphFormat.LineSpacingRule
        .LineSpacing = destStyle.ParagraphFormat.LineSpacing
        .Alignment = destStyle.ParagraphFormat.Alignment
        .WidowControl = destStyle.ParagraphFormat.WidowControl
        .KeepWithNext = destStyle.ParagraphFormat.KeepWithNext
        .KeepTogether = destStyle.ParagraphFormat.KeepTogether
        .PageBreakBefore = destStyle.ParagraphFormat.PageBreakBefore
        .NoLineNumber = destStyle.ParagraphFormat.NoLineNumber
        .Hyphenation = destStyle.ParagraphFormat.Hyphenation
        .FirstLineIndent = destStyle.ParagraphFormat.FirstLineIndent
        .OutlineLevel = destStyle.ParagraphFormat.OutlineLevel
        .CharacterUnitLeftIndent = destStyle.ParagraphFormat.CharacterUnitLeftIndent
        .CharacterUnitRightIndent = destStyle.ParagraphFormat.CharacterUnitRightIndent
        .CharacterUnitFirstLineIndent = destStyle.ParagraphFormat.CharacterUnitFirstLineIndent
        .LineUnitBefore = destStyle.ParagraphFormat.LineUnitBefore
        .LineUnitAfter = destStyle.ParagraphFormat.LineUnitAfter
        .MirrorIndents = destStyle.ParagraphFormat.MirrorIndents
        .TextboxTightWrap = destStyle.ParagraphFormat.TextboxTightWrap
        .CollapsedByDefault = destStyle.ParagraphFormat.CollapsedByDefault
        .AutoAdjustRightIndent = destStyle.ParagraphFormat.AutoAdjustRightIndent
        .DisableLineHeightGrid = destStyle.ParagraphFormat.DisableLineHeightGrid
        .FarEastLineBreakControl = destStyle.ParagraphFormat.FarEastLineBreakControl
        .WordWrap = destStyle.ParagraphFormat.WordWrap
        .HangingPunctuation = destStyle.ParagraphFormat.HangingPunctuation
        .HalfWidthPunctuationOnTopOfLine = destStyle.ParagraphFormat.HalfWidthPunctuationOnTopOfLine
        .AddSpaceBetweenFarEastAndAlpha = destStyle.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha
        .AddSpaceBetweenFarEastAndDigit = destStyle.ParagraphFormat.AddSpaceBetweenFarEastAndDigit
        .BaseLineAlignment = destStyle.ParagraphFormat.BaseLineAlignment
    End With
    sourceStyle.NoSpaceBetweenParagraphsOfSameStyle = destStyle.NoSpaceBetweenParagraphsOfSameStyle
    sourceStyle.ParagraphFormat.TabStops.ClearAll
    With sourceStyle.ParagraphFormat
        With .Shading
            .Texture = destStyle.ParagraphFormat.Shading.Texture
            .ForegroundPatternColor = destStyle.ParagraphFormat.Shading.ForegroundPatternColor
            .BackgroundPatternColor = destStyle.ParagraphFormat.Shading.BackgroundPatternColor
        End With
        .Borders(wdBorderLeft).LineStyle = destStyle.ParagraphFormat.Borders(wdBorderLeft).LineStyle
        .Borders(wdBorderRight).LineStyle = destStyle.ParagraphFormat.Borders(wdBorderRight).LineStyle
        .Borders(wdBorderTop).LineStyle = destStyle.ParagraphFormat.Borders(wdBorderTop).LineStyle
        .Borders(wdBorderBottom).LineStyle = destStyle.ParagraphFormat.Borders(wdBorderBottom).LineStyle
        With .Borders
            .DistanceFromTop = destStyle.ParagraphFormat.Borders.DistanceFromTop
            .DistanceFromLeft = destStyle.ParagraphFormat.Borders.DistanceFromLeft
            .DistanceFromBottom = destStyle.ParagraphFormat.Borders.DistanceFromBottom
            .DistanceFromRight = destStyle.ParagraphFormat.Borders.DistanceFromRight
            .Shadow = destStyle.ParagraphFormat.Borders.Shadow
        End With
    End With
    sourceStyle.LanguageID = destStyle.LanguageID
    sourceStyle.NoProofing = destStyle.NoProofing
    sourceStyle.LanguageID = destStyle.LanguageID
    sourceStyle.NoProofing = destStyle.NoProofing
    sourceStyle.Frame.Delete
End Sub

I'd like to find out the argument of the Application.CommandBars.ExecuteMso to do this update style mission just like following Tim's answer to easily and accurately accomplish with agility, however, I couldn't find it. Just found something like it(Options.UpdateStyleListBehavior property), but should be not this. Thus the only way to achieve it was to record a macro to simulate and reproduce all the relevant properties and methods. So if there are any omissions, please take note of them.

  • Before
    段落样式复制/重命名

  • After
    段落样式复制/重命名

huangapple
  • 本文由 发表于 2023年7月14日 03:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定