Consulting

Results 1 to 5 of 5

Thread: Converting a string to a Style Name

  1. #1

    Converting a string to a Style Name

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Style names are strings...
    Sub Demo()
    Dim Rng As Range
    With Selection
      Set Rng = .Range
      Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
      .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
        Text:="STYLEREF  """ & Rng.Paragraphs.First.Style & """ \s ", PreserveFormatting:=False
    End With
    End Sub
    Last edited by macropod; 12-10-2018 at 02:35 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks, Paul, for the code example. I created a document, stuck some text in and made one paragraph a Heading 1 style and then further down a Heading 2 style.

    I placed my cursor below the Heading 2 style.

    Following that I walked through your code using ALT+F8. When it got to the following lines I saw that by hovering my mouse over the code I've highlighted, It showed as "Heading 2", surrounded in double quotes. Just what I expected.
      .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _    
    Text:="STYLEREF  " & Rng.Paragraphs.First.Style & " \s ", PreserveFormatting:=False
    On completing the procedure, I was hoping that it would place the correct STYLEREF field in the document and perhaps showing the number "0".
    No such luck! It gave me an error as below:
    Error! Use the Home tab to apply to the text that you want to appear here.

    I then inspected the field it wrote and found the following

    {STYLEREF Heading 2 \s}
    I was expecting the Heading 2 to be surrounded by double quotes. Following this, I inspected the Field Options dialog box and found that it had selected "Default Paragraph Font" from the available list of items and had written in the textbox: STYLEREF 2 \s Heading. (It messed that one up!)

    Following that, I then manually amended the field code to read STYLEREF "Heading 2" \s - adding my own quotes around Heading 2 - pressed the appropriate keys and saw the amended field in the document. Pressed ALT+F9 again to see the result and it displayed what it should have done: a number "0"

    For some reason, from the code you wrote which appears to show the correct style format with double quotes, to getting it on the page, Word VBA seems to chew it up!

    Cannot think why it's playing up.

    Roderick

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    I omitted the double quotes required to enclose the Style name within them in the STYLEREF field. Corrected. Try the revised code.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Quote Originally Posted by macropod View Post
    I omitted the double quotes required to enclose the Style name within them in the STYLEREF field. Corrected. Try the revised code.
    Paul, that worked brilliantly. I learnt something there about these double quotes.

    Thank you so much.

    Roderick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •