none
applying style to selection.range is different from applying it to a normal range? RRS feed

  • Question

  • In  a paragraph of mine with myStyle I changed the formatting of a word.

    If I find (with Find object) the range of that new formatted word and want to reset the old style with

    myRange.Style = ActiveDocument.Styles(myStyle)

    nothing will happen.

    But if I do

    myRange.Select
    Selection.Style = ActiveDocument.Styles(myStyle)
    

    the old style is applied.

    Why the difference?

    Tuesday, November 1, 2016 7:05 AM

All replies

  • Hi,

    Use the following code, the results are the same.

    How do you get the range and style in your code? Could please share us the code for getting myRange and myStyle?

     

    Sub testStyle()
    Dim myStyle As String
    myStyle = "Heading 1"
    Dim rng As Range
    Set rng = ActiveDocument.Range(0, 10)
    rng.Style = ActiveDocument.Styles(myStyle)
    'rng.Select
    'Selection.Style = ActiveDocument.Styles(myStyle)
    End Sub



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 2, 2016 5:48 AM
    Moderator
  • Hi Celeste,

    try to write a small sentence in the first paragraph and set its style to one of your choice, I picked "Heading 2" (in Italian "Titolo 2", with blue font). Then select a word a make it red.

    Use the following code.

     Public Sub test()
       Dim rtemp As Range, rTesto As Range
         
       Set rTesto = ActiveDocument.Paragraphs(1).Range
       With rTesto.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Format = True
             With .Font
                .ColorIndex = wdRed
             End With
            .Wrap = wdFindStop
            If .Execute Then Set rtemp = ActiveDocument.Range(Start:=rTesto.Start, End:=rTesto.End)
            .ClearFormatting
            .Replacement.ClearFormatting
         End With
         'the following two lines ok
    '    rtemp.Select
    '    Selection.Style = ActiveDocument.Styles("Titolo 2")
         'the following will not set back to "Titolo 2" style
         rtemp.Style = ActiveDocument.Styles("Titolo 2")
    End Sub

    Ciao Lauro

    Wednesday, November 2, 2016 5:45 PM
  • Hi,

    In fact, the code would not work all the time. I suggest you do more tests and I think you would find sometimes they all work, sometimes they don’t work or sometimes using Selection works.

    In general, the reason is because Office stores the settings in different parts."Heading2" is a paragraph property and font color is a run property. You could see the pictures below. For different running, we would get different result. In my opinion, we could not control it by using VBA.

    The left is "This is a test" with style Heading2; the right is "This is a test" with style heading2 and "test" is in red.

    As for the reason why sometimes Selection.Style works and range.Style fails, I think, because it refers to the whole range when using range.Style to set style, but it refers to the selected range when using Selection.Style.

    You could try to write "This is a test" with Normal style and test the code below. 

    In my first reply, i got the same result because my string length is 10.

    Sub testStyle()
    Dim myStyle As String
    myStyle = "Heading 1"
    Dim rng As Range
    Set rng = ActiveDocument.Range(0, 10)
    rng.Style = ActiveDocument.Styles(myStyle)
    'rng.Select
    'Selection.Style = ActiveDocument.Styles(myStyle)
    End Sub


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 3, 2016 1:37 PM
    Moderator