简体   繁体   English

VBA Word-遍历段落以设置样式

[英]VBA Word - iterate through paragraphs to set style

I have a simple script that goes through and sets the style for all paragraphs beginning with a certain character. 我有一个简单的脚本,它以某种字符开头并设置了所有段落的样式。 Easy, I thought. 容易,我想。 It changes all the paragraphs so they have the same properties as the "Details" Style. 它将更改所有段落,使其具有与“详细信息”样式相同的属性。 But for some reason only the last paragraph ends up with "Details" as its style and all the ones before go back to "Normal". 但是出于某种原因,只有最后一段以“ Details”作为样式结尾,而所有段落都回到“ Normal”。 Here's my code so far: 到目前为止,这是我的代码:

Sub Format_doc()
Dim txt As String

For Each par In ActiveDocument.Paragraphs
    txt = par.Range.Text
    If Left(txt, 1) = "/" Then
        par.Style = "Details"
        par.Range.Text = Right(txt, Len(txt) - 1)
    End If
Next
End Sub

I'd like to keep them attached to the style because I toggle the "hidden" font property in another macro. 我想让它们与样式保持联系,因为我在另一个宏中切换了“隐藏”字体属性。 I'll need to toggle this hidden property for these paragraphs on-and-off several times and assigning a single paragraph style seemed like an easy solution. 我需要多次为这些段落切换此隐藏属性,并且分配单个段落样式似乎是一个简单的解决方案。 Here's the other code: 这是其他代码:

Sub Toggle_hidden()
    ActiveDocument.Styles("Details").Font.Hidden = Not ActiveDocument.Styles("Details").Font.Hidden
End Sub

Solutions? 解决方案? I'm working on Mac, but ultimately this will end up on a Windows. 我在Mac上工作,但最终它将在Windows上结束。

Your code works fine, here. 您的代码在这里工作正常。 But perhaps that's due to the version of MacWord... I tested with Office 2016 (Office 365 subscription). 但这也许是由于MacWord的版本...我在Office 2016(Office 365订阅)中进行了测试。

If it's not working for you it may have something to do with the way you're removing the / by basically replacing the paragraph's content. 如果对您不起作用,则可能与您通过基本替换段落内容的方式删除/有关。 This will also affect the paragraph mark, which is responsible for the paragraph formatting, including the style. 这也会影响段落标记,该段落标记负责段落格式(包括样式)的格式化。 Try the following, which explicitly removes the first character and leaves everything else intact: 请尝试以下操作,该操作将显式删除第一个字符并使其他所有内容保持不变:

Sub Format_doc()
Dim txt As String
Dim par As Word.Paragraph

For Each par In ActiveDocument.Paragraphs
    txt = par.Range.Text
    If Left(txt, 1) = "/" Then
        par.Style = "Details"
        'par.Range.Text = Right(txt, Len(txt) - 1)
        par.Range.Characters(1).Delete
    End If
Next
End Sub

Here's a different approach that should also work - and be somewhat faster. 这是一种应该也可以起作用的不同方法,并且速度更快。

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p/"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = False
    .Execute
  End With
  Do While .Find.Found
    With .Duplicate
      .Start = .Start + 1
      .End = .Paragraphs(1).Style = "Details"
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

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

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