Roderick
12-08-2018, 11:35 AM
In a document I am using Word's built-in Heading styles. The code below "runs" up to the first Heading style it finds and determines whether it is a Heading 1 or 2, 3, etc, down to Heading 6.
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = VBA.Right(figRng.Paragraphs.First.Style, 1)
End With
'figLvl is an integer
If figLvl = 1 Then
'myNewStyle is a string
myNewStyle = "Heading 1"
ElseIf figLvl = 2 Then
myNewStyle = "Heading 2"
End If
The next piece of code - which I recorded, inserts a STYLEREF field to number the sequence:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 2"" \s ", PreserveFormatting:=True
As can be seen, figRng found that the style was a Heading 2 and therefore figLvl reads "2" and myNewStyle now reads as Heading 2.
The text in red above was entered as a style when I recorded the field structure but what I want to do is replace it with a style name based on myNewStyle as seen in the top piece of code.
I've taken the STYLEREF field apart in all sorts of ways to try and achieve this but it insists on giving me an error whatever I try and do.
Here is the code for the whole procedure including the STYLEREF field which I recorded:
Sub InsertNewStyleRef()
Dim myNewStyle
Dim figLvl As Integer
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = VBA.Right(figRng.Paragraphs.First.Style, 1)
End With
If figLvl = 1 Then
myNewStyle = "Heading 1"
ElseIf figLvl = 2 Then
myNewStyle = "Heading 2"
End If
'and so on down the Heading styles
'now add the STYLEREF field
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 2"" \s ", PreserveFormatting:=True
End Sub
Is there a way to convert a string to a style name, please?
Roderick
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = VBA.Right(figRng.Paragraphs.First.Style, 1)
End With
'figLvl is an integer
If figLvl = 1 Then
'myNewStyle is a string
myNewStyle = "Heading 1"
ElseIf figLvl = 2 Then
myNewStyle = "Heading 2"
End If
The next piece of code - which I recorded, inserts a STYLEREF field to number the sequence:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 2"" \s ", PreserveFormatting:=True
As can be seen, figRng found that the style was a Heading 2 and therefore figLvl reads "2" and myNewStyle now reads as Heading 2.
The text in red above was entered as a style when I recorded the field structure but what I want to do is replace it with a style name based on myNewStyle as seen in the top piece of code.
I've taken the STYLEREF field apart in all sorts of ways to try and achieve this but it insists on giving me an error whatever I try and do.
Here is the code for the whole procedure including the STYLEREF field which I recorded:
Sub InsertNewStyleRef()
Dim myNewStyle
Dim figLvl As Integer
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = VBA.Right(figRng.Paragraphs.First.Style, 1)
End With
If figLvl = 1 Then
myNewStyle = "Heading 1"
ElseIf figLvl = 2 Then
myNewStyle = "Heading 2"
End If
'and so on down the Heading styles
'now add the STYLEREF field
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 2"" \s ", PreserveFormatting:=True
End Sub
Is there a way to convert a string to a style name, please?
Roderick