none
how to handle non-breaking hyphen in text between range in word? RRS feed

  • Question

  • I have some text with non-breaking hyphen in word. I need that text as it is in output. But am getting text with non-breaking hyphen removed.

    Word.Range rangeObj = _wordDoc.Range(ref x, ref y);
    txt += rangeObj.Text;

    I am using the above code. And in txt variable I am getting output but with non breaking hyphen removed.  Thanks in advance.

    Monday, March 7, 2016 7:03 AM

Answers

  • I got 1 solution for this. we can replace non-breaking hyphen with normal hyphen by using below line of code:

    txt = Regex.Replace(txt, "[\u001e|\u2011|\u2013|\u2014]", "-");

    • Marked as answer by James Shokeen Wednesday, March 9, 2016 12:20 PM
    Wednesday, March 9, 2016 12:19 PM

All replies

  • Hi James,

    According to your description, your case more related to word development, I will move your case to word forum for better support.

    Best regards,

    Kristin


    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.

    Tuesday, March 8, 2016 1:59 AM
  • Hi James,

    This is expected behavior. Because in the Word, the non-breaking hyphen is a special code instead of "-" character. We can reflect the Word document with Open XML SDK 2.5 Productivity Tool which shift with Open XML SDK 2.5.

    Here is the Open XML element for your reference.

    Words with non-breaking hyphen:

     <w:r>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t xml:space="preserve">                                                                                                                                                                Hello</w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:noBreakHyphen />
        <w:t>World</w:t>
      </w:r>
    

    Words with '-' character:

    <w:r>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t xml:space="preserve">                                                                                                                                                                Hello</w:t>
      </w:r>
      <w:bookmarkStart w:name="_GoBack" w:id="0" />
      <w:r>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>-</w:t>
      </w:r>
      <w:bookmarkEnd w:id="0" />
      <w:r>
        <w:rPr>
          <w:lang w:val="en-US" />
        </w:rPr>
        <w:t>World</w:t>
      </w:r>
    

    As a workaround, we can copy the text into a temporary document and replace the non-breaking hyphen with the character we want to display.

    In addition, if you have developing the solution, I suggest that you record a macro to get a quick sample code for the reference.

    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, March 9, 2016 12:03 PM
    Moderator
  • I got 1 solution for this. we can replace non-breaking hyphen with normal hyphen by using below line of code:

    txt = Regex.Replace(txt, "[\u001e|\u2011|\u2013|\u2014]", "-");

    • Marked as answer by James Shokeen Wednesday, March 9, 2016 12:20 PM
    Wednesday, March 9, 2016 12:19 PM
  • Thanks for the details Fei.
    Wednesday, March 9, 2016 12:20 PM