none
Save Word text formatting in RichtextBox object RRS feed

  • Question

  • In Word VSTO Add-in I want to save text formatting in richtextbox object and then get that text as richtextbox object.Rtf.

    In short I want to save word text with all formatting in html format.

    I have tried below code:

    Word.Range rng = docm.Range(docm.tables[1].cell(1,1).range.start, docm.tables[1].cell(1,1).range.end - 1);

    rng.Copy();

    RichTextBox rtxt = new RichTextBox();

    txt.Paste();//after that I convert that rtf code to html code

    messageBox.show("text = "+ rtxt.Rtf);

    But this code does not work on all machines.

    I have two machines with same configuration(same OS and Office) but it works only one machine. 

    Please help me how to convert my text formatting into html.

     
    Thursday, September 22, 2016 7:44 AM

Answers

  • Hi Saurabh Singh Jadon,

    According to your description, I am not able to reproduce this issue. Do you use same document to reproduce this issue, what is difference when you got different result, which version is Word?

    In addition after you modify your code like below, what is you got:
    if(Clipboard.ContainsText(TextDataFormat.Rtf))
    {
        rtxt.Paste();//after that I convert that rtf code to html code
    
        MessageBox.Show("text = " + rtxt.Rtf);
    }

    Thanks for your understanding.
    • Proposed as answer by David_JunFeng Friday, September 30, 2016 2:18 AM
    • Marked as answer by David_JunFeng Friday, September 30, 2016 2:18 AM
    • Unmarked as answer by David_JunFeng Friday, September 30, 2016 2:19 AM
    • Marked as answer by David_JunFeng Tuesday, October 11, 2016 1:19 AM
    Monday, September 26, 2016 8:37 AM

All replies

  • >>>I have two machines with same configuration(same OS and Office) but it works only one machine. 

    According to your description, I have made a sample to try to reproduce this issue, but I just get this result:

    Word Com Object do not support to convert rtf code to html code, what C# code do you convert rtf code to html code? I think that this issue is related to C#.

    Could you provide more information about your issue, for example exception, screenshot etc., that will help us reproduce and resolve it.

    Thanks for your understanding.

    Friday, September 23, 2016 4:48 AM
  • Thanks for your response.

    I have tried my code for text :  {sw_bit_enable=F2}

    It gives the below output:

    On machine1:

    range.text: {sw_bit_enable=F2} and range.rtf: {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    {\*\generator Riched20 12.0.6606.1000;}\viewkind4\uc1
    \pard\f0\fs17\par
    }

    On machine2:(working)

    range.text: {sw_bit_enable=F2} and range.rtf: {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
    {\colortbl ;\red227\green108\blue10;}
    {\*\generator Riched20 15.0.4737}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 
    \pard\cf1\b\f0\fs24\{sw_bit_enable=F2\}\cf0\b0\f1\fs17\par
    }


    Friday, September 23, 2016 11:01 AM
  • After that I convert that rtf code to html code but proper rtf is not generated on all machines.
    Friday, September 23, 2016 11:03 AM
  • any update on this...

    Monday, September 26, 2016 6:19 AM
  • Hi Saurabh Singh Jadon,

    According to your description, I am not able to reproduce this issue. Do you use same document to reproduce this issue, what is difference when you got different result, which version is Word?

    In addition after you modify your code like below, what is you got:
    if(Clipboard.ContainsText(TextDataFormat.Rtf))
    {
        rtxt.Paste();//after that I convert that rtf code to html code
    
        MessageBox.Show("text = " + rtxt.Rtf);
    }

    Thanks for your understanding.
    • Proposed as answer by David_JunFeng Friday, September 30, 2016 2:18 AM
    • Marked as answer by David_JunFeng Friday, September 30, 2016 2:18 AM
    • Unmarked as answer by David_JunFeng Friday, September 30, 2016 2:19 AM
    • Marked as answer by David_JunFeng Tuesday, October 11, 2016 1:19 AM
    Monday, September 26, 2016 8:37 AM