Reputation: 33
I have a code that loops through the paragraphs of a document, and reapplies the heading style. This is in order to fix the numbering inconsistency after copy-pasting headings from different documents.
In my file I just have the following example:
For Each p In ThisDocument.Paragraphs
If p.Style = "Heading 1" Then
p.Style = "Heading 1"
End If
Next p
This solves the issue, but the problem is if somebody has Word running in a different language, "Heading 1" is not applicable anymore. I tried with wdstyleheading1
as well, but the p.Style
always reads out the actual name of the style, not an enumeration.
I thought of p.Style.ListLevelNumber
, but that seems to be too vague, and might mess up other lists in the document.
Is there a way to read or reference it in a language independent way? Is there perhaps a better solution altogether for the numbering problem?
Upvotes: 0
Views: 175
Reputation: 13490
Actually, if your style definitions are correct in the attached template, you don't even need a macro for this. All you need do is check the 'automatically update document styles' option, found under Developer|Document Template>Templates. With that set, all you need do is save, close, then re-open the document.
Otherwise, using Find/Replace for all 9 Heading Styles:
Sub SetHeadingLanguage()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
'Style enumerations run from -2 (wdStyleHeading1) to -10 (wdStyleHeading9)
For i = -2 To -10 Step -1
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Format = True
.Wrap = wdFindContinue
.Style = i
.Replacement.Style = i
.Execute Replace:=wdReplaceAll
End With
Next
End With
Application.ScreenUpdating = True
MsgBox "Done."
End Sub
Upvotes: 0
Reputation: 83
you can get the local name for a style with .NameLocal. It's kind of a workaround because I don't think you can actually compare a paragraphs .Style with the enum, but this should do the trick.
For Each p In ThisDocument.Paragraphs
If p.Style = ActiveDocument.Styles(wdStyleHeading1).NameLocal Then
p.Style = wdStyleHeading1
End If
Next p
Upvotes: 1
Reputation: 13490
As simple as:
For Each p In ThisDocument.Paragraphs
If p.Style = wdStyleHeading1 Then
p.Style = wdStyleHeading1
End If
Next p
That said, using Find/Replace is way faster than looping through all paragraphs.
Upvotes: 0