none
Find and Clear hyperlink style in a paragraph RRS feed

  • Question

  • When we create hyperlinks in word document, sometimes some character after the hyperlink, like dot, space, at times some word get the hyperlink style. 

    And they are NOT actually hyperlinks.

    I would like to find these characters/words which has got hyperlink style (but actually nor hyperlink) in a paragraph, and clear the hyperlink style.

    In other words, get all the range in a paragraph/document  that has the hyperlink style set, and clear hyperlink style if it is not an actual hyperlink

    We use large word documents, and we want to programmatically  find them and clear them, without affecting performance.

    Thanks,

    Ramesh

    Wednesday, July 27, 2016 2:11 PM

All replies

  • Hello Ramesh,

    VSTO doesn't provide anything for that. I'd recommend asking Word specific questions on the Word for Developers forum instead.


    [custom.development]

    Wednesday, July 27, 2016 1:59 PM
  • Hi RameshSubburaj,

    you want to find the hyperlink style and want to remove it from text.

    but in word object model there is no any predefine style like this so that we can directly search it.

    if you want to search the formatting like this then we know that generally it contains the blue color and underline.

    so we can try to search based on this formatting and if you find it then you can change the formatting of that text to normal.

    following is the demo example code to get the idea how you can achieve it.

    you need to modify it to get the desired result.

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    '
        Selection.Find.ClearFormatting
        With Selection.Find.Font
            .Underline = wdUnderlineSingle
            .Color = -671023105
        End With
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = True
            .Font.Underline = wdUnderlineNone
            .Font.ColorIndex = wdBlack
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
       
    End Sub

    Regards

    Deepak


    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.

    Friday, July 29, 2016 3:13 AM
    Moderator
  • Hi Deepak,
    Thanks for writing the macro for me. Am writing a word addin c# code. I was able to find the style by the below code, I could not replace the format.

    Let me know if you can help with, what is wrong in replacement.

        Find styleFind = paragraph.Range.Find;
                        styleFind.Format = true;
                        object oMissing = Type.Missing;
                        object oReplace = WdReplace.wdReplaceAll;
                        object ostyle = WdBuiltinStyle.wdStyleHyperlink;
                        styleFind.set_Style(ref ostyle);
                        styleFind.Replacement.set_Style(WdBuiltinStyle.wdStyleNormal);
                        bool found=     styleFind.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oReplace , ref oMissing, ref oMissing, ref oMissing, ref oMissing) ;

    Thanks,

    Ramesh


    Friday, July 29, 2016 6:58 AM
  • Hi RameshSubburaj,

    you had mentioned that you can find the Hyperlink formatting. but you are not able to Replace with Normal Style.

    but in styleFind.Execute I find that you had passed the parameter as missing in Format.

    why don't you pass "True" there to find the formatting.

    Find.Execute Method (Word)

    Regards

    Deepak


    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.

    Monday, August 1, 2016 8:32 AM
    Moderator
  • Hi Ramesh,

    Simply you can find replace Styles by using recorded VBA macro.

    Sub FindHyperlinkStyle()
        Selection.Find.ClearFormatting
        Selection.Find.Style = ActiveDocument.Styles("Hyperlink")
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
    End Sub
    
    Hope this will helpful for you.


    .

    Tuesday, August 2, 2016 11:14 AM