none
Difficulty removing characters from the start of a numbered paragraph RRS feed

  • Question

  • Hello,

    I am having a problem with the below little routine that I wrote to round-trip numbered paragraphs into chevronated paragraphs and then to return back to numbered paragraphs.

    I start with the following:

    1. Level 1
     1.1. Level 2a
      1.1.A. Level 3
     1.2. Level 2b

    When I run the code on this, it produces the following, which is correct:

    Level 1
    >Level 2a
    >>Level 3
    >Level 2b

    When I run the code on this result, it produces the following, which is incorrect:

    1. Level 1
     1.1. >Level 2a
      1.1.A. >>Level 3
     1.2. >Level 2b

    This is incorrect because of the existence of the chevrons. However, any attempt I make to get rid of the chevrons within the loop gives me unpredictable results. I tried to do L_V_Scalar_Range_Generic.Text.Replace(“>”,””), but that removes more than just the chevrons, and it seems to have an impact on the indentation. I think I am missing something simple. How would you remove those chevrons?

    Many thanks in advance,

    Keith

    FriendSharedSubF_S_StyleConvertChevrons(ByValV_V_Scalar_Range_Generic AsRange, ByValV_V_Scalar_Document_Generic AsDocument)

    '#[3:HowRK00<HowRK00:0.25:The indentation for level 2 and onwards is 1 level too many for de-chevronation.]


    IfV_V_Scalar_Range_Generic.Start <> V_V_Scalar_Range_Generic.End Then


    ForEachL_V_Scalar_Paragraph_Item AsParagraphInV_V_Scalar_Range_Generic.Paragraphs

    DimL_V_Scalar_Range_Generic AsRange= L_V_Scalar_Paragraph_Item.Range

    DimL_V_Scalar_Style_Generic AsStyle= DirectCast(L_V_Scalar_Range_Generic.Style, Style)

    IfL_V_Scalar_Style_Generic.NameLocal = V_V_Scalar_Document_Generic.Styles.Item("Normal").NameLocal Then


    L_V_Scalar_Range_Generic.Style =

    C_A_G_C_U_Constant.G_C_Scalar_String_OrdinaryListStyleText

    DimL_V_Scalar_Integer_ChevronCount AsInteger= C_A_H_CommonCoreCore.U_A_G_C_U_String.G_F_Scalar_Integer_CountCharacters(L_V_Scalar_Range_Generic.Text, ">"c)

    ForL_V_Scalar_Integer_ListLevelNumberCurrent AsInteger= 1 ToL_V_Scalar_Integer_ChevronCount

    L_V_Scalar_Paragraph_Item.Range.ListFormat.ListIndent()

    Next


    Else


    ConstL_C_Scalar_Integer_NonChevronatedLevelsCount AsInteger= 1

    DimL_V_Scalar_Integer_ListLevelNumberMaximum AsInteger= M_F_Scalar_Integer_GetListLevelFromParagraph(L_V_Scalar_Range_Generic) - L_C_Scalar_Integer_NonChevronatedLevelsCount

    IfL_V_Scalar_Integer_ListLevelNumberMaximum >= 0 Then


    L_V_Scalar_Range_Generic.Style = V_V_Scalar_Document_Generic.Styles.Item(

    "Normal").NameLocal

    DimL_V_Scalar_String_ChevronLevel AsString= NewString(">"c, L_V_Scalar_Integer_ListLevelNumberMaximum)

    L_V_Scalar_Range_Generic.InsertBefore(L_V_Scalar_String_ChevronLevel)

    EndIf


    EndIf


    Next


    EndIf


    EndSub


    R K Howard

    Tuesday, November 25, 2014 5:57 PM

Answers

  • Hi. Thanks for your feedback. Manipulating text within a range within a loop seems quite precarious and fiddly. After a lot of trial and error, I got the following code to work. I had to change a For Next loop to a Do While loop, and I had to add a couple of nasty If Then conditions.

    FriendSharedSubF_S_StyleConvertChevrons(ByValV_V_Scalar_Selection_Generic AsSelection)

    IfV_V_Scalar_Selection_Generic.Start <> V_V_Scalar_Selection_Generic.End Then


    DimL_V_Scalar_Integer_ParagraphCount AsInteger= V_V_Scalar_Selection_Generic.Paragraphs.Count

    DimL_V_Scalar_Integer_ParagraphCurrent AsInteger= 1

    DoWhileL_V_Scalar_Integer_ParagraphCurrent <= L_V_Scalar_Integer_ParagraphCount

    DimL_V_Scalar_Range_Generic AsRange= V_V_Scalar_Selection_Generic.Paragraphs(L_V_Scalar_Integer_ParagraphCurrent).Range

    DimL_V_Scalar_Style_Generic AsStyle= DirectCast(L_V_Scalar_Range_Generic.Style, Style)

    IfL_V_Scalar_Style_Generic.NameLocal = "Normal"Then


    DimL_V_Scalar_Integer_ChevronCount AsInteger= C_A_H_CommonCoreCore.U_A_G_C_U_String.G_F_Scalar_Integer_CountCharacters(L_V_Scalar_Range_Generic.Text.Trim, ">"c)

    DimL_V_Scalar_String_ParagraphOriginal AsString= L_V_Scalar_Range_Generic.Text

    IfL_V_Scalar_String_ParagraphOriginal.Trim.StartsWith(">") Then


    DimL_V_Scalar_String_ParagraphReplacement AsString= L_V_Scalar_String_ParagraphOriginal.Substring(L_V_Scalar_Integer_ChevronCount)

    L_V_Scalar_Range_Generic.Text = L_V_Scalar_String_ParagraphReplacement

    EndIf


    L_V_Scalar_Range_Generic.Style =

    C_A_G_C_U_Constant.G_C_Scalar_String_OrdinaryListStyleText

    L_V_Scalar_Range_Generic.ListFormat.ListLevelNumber = L_V_Scalar_Integer_ChevronCount + 1

    IfL_V_Scalar_Integer_ParagraphCurrent = L_V_Scalar_Integer_ParagraphCount Then


    L_V_Scalar_Range_Generic.Application.Selection.MoveDown(

    WdUnits.wdParagraph, 1, WdMovementType.wdExtend)

    EndIf


    Else


    ConstL_C_Scalar_Integer_NonChevronatedLevelsCount AsInteger= 1

    DimL_V_Scalar_Integer_ListLevelNumberMaximum AsInteger= M_F_Scalar_Integer_GetListLevelFromParagraph(L_V_Scalar_Range_Generic) - L_C_Scalar_Integer_NonChevronatedLevelsCount

    IfL_V_Scalar_Integer_ListLevelNumberMaximum >= 0 Then


    L_V_Scalar_Range_Generic.Style =

    "Normal"


    DimL_V_Scalar_String_ChevronLevel AsString= NewString(">"c, L_V_Scalar_Integer_ListLevelNumberMaximum)

    L_V_Scalar_Range_Generic.InsertBefore(L_V_Scalar_String_ChevronLevel)

    EndIf


    EndIf


    L_V_Scalar_Integer_ParagraphCurrent += 1

    Loop


    EndIf


    EndSub


    R K Howard

    Thursday, November 27, 2014 6:15 PM

All replies

  • Hi Keith,

    Did you want to remove ">"  in the document?

    I failed to run the methoed above, would you mind sharing with us a workable sample?

    Also I test the code below to remove the ">" in the document, it works well for me:

    Sub ReplaceChevrons()
    Debug.Print Replace(Application.Selection.Range.Text, ">", "")
    
    End Sub

    Here is the screenshot:

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 26, 2014 6:49 AM
    Moderator
  • Hi. Thanks for your feedback. Manipulating text within a range within a loop seems quite precarious and fiddly. After a lot of trial and error, I got the following code to work. I had to change a For Next loop to a Do While loop, and I had to add a couple of nasty If Then conditions.

    FriendSharedSubF_S_StyleConvertChevrons(ByValV_V_Scalar_Selection_Generic AsSelection)

    IfV_V_Scalar_Selection_Generic.Start <> V_V_Scalar_Selection_Generic.End Then


    DimL_V_Scalar_Integer_ParagraphCount AsInteger= V_V_Scalar_Selection_Generic.Paragraphs.Count

    DimL_V_Scalar_Integer_ParagraphCurrent AsInteger= 1

    DoWhileL_V_Scalar_Integer_ParagraphCurrent <= L_V_Scalar_Integer_ParagraphCount

    DimL_V_Scalar_Range_Generic AsRange= V_V_Scalar_Selection_Generic.Paragraphs(L_V_Scalar_Integer_ParagraphCurrent).Range

    DimL_V_Scalar_Style_Generic AsStyle= DirectCast(L_V_Scalar_Range_Generic.Style, Style)

    IfL_V_Scalar_Style_Generic.NameLocal = "Normal"Then


    DimL_V_Scalar_Integer_ChevronCount AsInteger= C_A_H_CommonCoreCore.U_A_G_C_U_String.G_F_Scalar_Integer_CountCharacters(L_V_Scalar_Range_Generic.Text.Trim, ">"c)

    DimL_V_Scalar_String_ParagraphOriginal AsString= L_V_Scalar_Range_Generic.Text

    IfL_V_Scalar_String_ParagraphOriginal.Trim.StartsWith(">") Then


    DimL_V_Scalar_String_ParagraphReplacement AsString= L_V_Scalar_String_ParagraphOriginal.Substring(L_V_Scalar_Integer_ChevronCount)

    L_V_Scalar_Range_Generic.Text = L_V_Scalar_String_ParagraphReplacement

    EndIf


    L_V_Scalar_Range_Generic.Style =

    C_A_G_C_U_Constant.G_C_Scalar_String_OrdinaryListStyleText

    L_V_Scalar_Range_Generic.ListFormat.ListLevelNumber = L_V_Scalar_Integer_ChevronCount + 1

    IfL_V_Scalar_Integer_ParagraphCurrent = L_V_Scalar_Integer_ParagraphCount Then


    L_V_Scalar_Range_Generic.Application.Selection.MoveDown(

    WdUnits.wdParagraph, 1, WdMovementType.wdExtend)

    EndIf


    Else


    ConstL_C_Scalar_Integer_NonChevronatedLevelsCount AsInteger= 1

    DimL_V_Scalar_Integer_ListLevelNumberMaximum AsInteger= M_F_Scalar_Integer_GetListLevelFromParagraph(L_V_Scalar_Range_Generic) - L_C_Scalar_Integer_NonChevronatedLevelsCount

    IfL_V_Scalar_Integer_ListLevelNumberMaximum >= 0 Then


    L_V_Scalar_Range_Generic.Style =

    "Normal"


    DimL_V_Scalar_String_ChevronLevel AsString= NewString(">"c, L_V_Scalar_Integer_ListLevelNumberMaximum)

    L_V_Scalar_Range_Generic.InsertBefore(L_V_Scalar_String_ChevronLevel)

    EndIf


    EndIf


    L_V_Scalar_Integer_ParagraphCurrent += 1

    Loop


    EndIf


    EndSub


    R K Howard

    Thursday, November 27, 2014 6:15 PM