2

I intent to load a word document into a richtextbox.

I have the below code. Which yes is working, but it is taking waaaay to long to load.

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word Documents|*.doc; *.docx";

if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
    object miss = System.Reflection.Missing.Value;
    object path = ofd.FileName;
    object readOnly = true;
    Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly,
        ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
        ref miss, ref miss, ref miss, ref miss);
    string totaltext = "";


    for (int i = 0; i < docs.Paragraphs.Count; i++)
    {
        totaltext += "\t " + docs.Paragraphs[i + 1].Range.Text.ToString();
    }

    richTextBox1.Text = totaltext;
}

It takes about 2 mins to load a 3 page test document, and foreer to load a 60+ page document.

It might have something to do with the for loop. Kindly assist me with a way to IMPROVE ON THE SPEED.

BiKe
  • 83
  • 1
  • 12
  • 1
    For a start, I'd recommend appending the `totalText` into `StringBuilder`. Alternatively, save the document as RTF and load directly into the `RichTextBox` - it's very slow on the setter – DiskJunky Sep 25 '17 at 14:03

1 Answers1

0

Instead of

string totaltext = "";

for (int i = 0; i < docs.Paragraphs.Count; i++)
{
    totaltext += "\t " + docs.Paragraphs[i + 1].Range.Text.ToString();
}

richTextBox1.Text = totaltext;

Use this:

var totaltextBuilder = new StringBuilder();

for (int i = 0; i < docs.Paragraphs.Count; i++)
{
    totaltextBuilder.Append("\t " + docs.Paragraphs[i + 1].Range.Text.ToString());
}

richTextBox1.Text = totaltextBuilder.ToString();

From MSDN:

For routines that perform extensive string manipulation (such as apps that modify a string numerous times in a loop), modifying a string repeatedly can exact a significant performance penalty. The alternative is to use StringBuilder, which is a mutable string class. Mutability means that once an instance of the class has been created, it can be modified by appending, removing, replacing, or inserting characters. A StringBuilder object maintains a buffer to accommodate expansions to the string. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is then appended to the new buffer.

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
  • Thank you for your help, but unfortunately the string builder has not made the loading any faster. Any other suggestions or extra information – BiKe Sep 25 '17 at 14:25
  • Try @DiskJunky's suggestion: save the document as RTF and then load it directly into the `RichTextBox`. – Anderson Pimentel Sep 25 '17 at 14:28
  • I have used @DiskJunky (thank you) suggestion, the solution is much faster. If at all you get another way to fasten the .doc file load do not hesitate to share. – BiKe Sep 25 '17 at 16:13