none
Applying or removing the Hyperlink style by searching and replacing the underlying XML in the document RRS feed

  • Question

  • Search
    <w:rStyle w:val="Hyperlink"/></w:rPr><w:t>http://www.bbc.co.uk</w:t></w:r></w:hyperlink>
    Replace
    http://www.bbc.co.uk

    Hello,

    I am trying to search and replace inside the XML within a Word .docx file. I am trying to eliminate the hyperlink style but retain the website address within the hyperlink. From what I can tell, the above search string specifies the start and end hyperlink tags, so I was trying replace the above search string with simply the replacement website address. When I ran this replacement, it corrupted the Word document. I then tried to remove retain various tags such as </w:rPr> or <w:t> or </w:t> or </w:r>, within the replacement string. Each of these attempts also corrupted the document.
    Does anyone have a clue as to what I am doing wrong
    (I need to run this replacement over many documents, so I cannot use the Word user interface.)

    Many thanks.
    Keith


    R K Howard

    Sunday, November 23, 2014 1:49 PM

Answers

  • Hello Keith,

    If you just want to remove the hyperlink stype but not to change the hyperlink to plain text, you can change the style from "Hyperlink" to "Normal":

    <w:rStyle w:val="Normal"/></w:rPr><w:t>http://www.bbc.co.uk</w:t></w:r></w:hyperlink>

    For how to change this using Open XML SDK, please take a look at the answer in this thread:

    var hl = hLink.Where(h => h.Id == hRel.Id).FirstOrDefault();
    string label = hl.InnerText;
    mainPart.DeleteReferenceRelationship(hRel.Id);
    var parent = hl.Parent;
    Run run = parent.AppendChild(new Run(hl.ChildElements[0].OuterXml.Replace("val=\"Hyperlink\"", "val=\"Normal\"")));
    hl.Remove();

    If you want to permanently remove the hyperlink from the text, then you can delete the relationship between the text and the hyperlink, check this sample code:

    MainDocumentPart mainPart = doc.MainDocumentPart;
    Hyperlink hLink = mainPart.Document.Body.Descendants<Hyperlink>().FirstOrDefault();
    if (hLink != null)
    {
    	// get hyperlink's relation Id (where path stores)
    	string relationId = hLink.Id;
    	if (relationId != string.Empty)
    	{
    		// get current relation
    		HyperlinkRelationship hr = mainPart.HyperlinkRelationships.Where(a => a.Id == relationId).FirstOrDefault();
    		if (hr != null)
    		// remove current relation
    		{ mainPart.DeleteReferenceRelationship(hr); }
    	}
    }
    // apply changes
    doc.Close();


    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, November 24, 2014 5:38 AM
    Moderator
  • Thanks. That gave me a clue. I was able to solve my problem.

    I removed the hyperlink style with the following RegEx

    Search:

    <w:rPr><w:rStyle w:val="+[0-9a-zA-Z_]"/></w:rPr><w:t>+1[a-zA-Z\\]

    Replace:

    <w:t>%2

    Then I can re-implement the hyperlink style again with e.g.,

    Search:

    <w:r><w:t>http://          

    Replace:

    <w:r><w:rPr><w:rStyle w:val="Hyperlink"/></w:rPr><w:t>http://             


    R K Howard

    Thursday, November 27, 2014 5:45 PM

All replies

  • Hello Keith,

    If you just want to remove the hyperlink stype but not to change the hyperlink to plain text, you can change the style from "Hyperlink" to "Normal":

    <w:rStyle w:val="Normal"/></w:rPr><w:t>http://www.bbc.co.uk</w:t></w:r></w:hyperlink>

    For how to change this using Open XML SDK, please take a look at the answer in this thread:

    var hl = hLink.Where(h => h.Id == hRel.Id).FirstOrDefault();
    string label = hl.InnerText;
    mainPart.DeleteReferenceRelationship(hRel.Id);
    var parent = hl.Parent;
    Run run = parent.AppendChild(new Run(hl.ChildElements[0].OuterXml.Replace("val=\"Hyperlink\"", "val=\"Normal\"")));
    hl.Remove();

    If you want to permanently remove the hyperlink from the text, then you can delete the relationship between the text and the hyperlink, check this sample code:

    MainDocumentPart mainPart = doc.MainDocumentPart;
    Hyperlink hLink = mainPart.Document.Body.Descendants<Hyperlink>().FirstOrDefault();
    if (hLink != null)
    {
    	// get hyperlink's relation Id (where path stores)
    	string relationId = hLink.Id;
    	if (relationId != string.Empty)
    	{
    		// get current relation
    		HyperlinkRelationship hr = mainPart.HyperlinkRelationships.Where(a => a.Id == relationId).FirstOrDefault();
    		if (hr != null)
    		// remove current relation
    		{ mainPart.DeleteReferenceRelationship(hr); }
    	}
    }
    // apply changes
    doc.Close();


    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, November 24, 2014 5:38 AM
    Moderator
  • Thanks. That gave me a clue. I was able to solve my problem.

    I removed the hyperlink style with the following RegEx

    Search:

    <w:rPr><w:rStyle w:val="+[0-9a-zA-Z_]"/></w:rPr><w:t>+1[a-zA-Z\\]

    Replace:

    <w:t>%2

    Then I can re-implement the hyperlink style again with e.g.,

    Search:

    <w:r><w:t>http://          

    Replace:

    <w:r><w:rPr><w:rStyle w:val="Hyperlink"/></w:rPr><w:t>http://             


    R K Howard

    Thursday, November 27, 2014 5:45 PM