Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:32:05+00:00 2026-05-25T01:32:05+00:00

I am working on MS Word application capturing tool in which we capture selected

  • 0

I am working on MS Word application capturing tool in which we capture selected contents via some keystrokes. All things done.
Now in selected range i would like to capture text as html for example

Text = Hello There. How are you ?

I have done above query by below code sample

string str = string.Empty;
Word.Range rngFindBold =
    ((Word.DocumentClass)(wh.Document)).ActiveWindow.Selection.Range;
rngFindBold.Find.Font.Bold = -1;
foreach (Word.Range rngWord in rngFindBold.Words)
{
    if (rngWord.Bold != 0)
    {
        // rngWord.Bold = 0;
        //rngWord.Text = "<b>" + rngWord.Text + "</b>";
        str += "<b>" + rngWord.Text + "</b>";
    }
    else if (rngWord.Italic != 0)
    {
        // rngWord.Italic = 0;
        //  rngWord.Text = "<i>" + rngWord.Text + "</i>";
        str += "<i>" + rngWord.Text + "</i>";
    }
    else if (rngWord.Underline != 0)
    {
        //  rngWord.Underline = 0;
        //  rngWord.Text = "<u>" + rngWord.Text + "</u>";
        str += "<u>" + rngWord.Text + "</u>";
    }
    else
    {
        str += rngWord.Text;
    }
}

Result = Hello &lt;b&gt;There&lt;/b&gt;&lt;b&gt;. &lt;/b&gt;&lt;b&gt;How &lt;/b&gt;are &lt;i&gt;you &lt;/i&gt;&lt;i&gt;?&lt;/i&gt;

Above code takes more time when selecting multiline content

How can i optimize above code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:32:06+00:00Added an answer on May 25, 2026 at 1:32 am

    Instead of using += use a StringBuilder object. This handles string operations much more efficient.

    That would change your code into :

    StringBuilder builder = new StringBuilder();
    Word.Range rngFindBold =
    ((Word.DocumentClass)(wh.Document)).ActiveWindow.Selection.Range;
    rngFindBold.Find.Font.Bold = -1;
    foreach (Word.Range rngWord in rngFindBold.Words)
    {
        if (rngWord.Bold != 0)
        {
            builder.Append("<b>" + rngWord.Text + "</b>");
        }
        else if (rngWord.Italic != 0)
        {
            builder.Append("<i>" + rngWord.Text + "</i>");
        }
        else if (rngWord.Underline != 0)
        {
            builder.Append("<u>" + rngWord.Text + "</u>");
        }
        else
        {
            builder.Append(rngWord.Text);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application which walks through every file in some directories and
I working with legacy (ancient is more appropriate word) application which use temp tables
Working with python interactively, it's sometimes necessary to display a result which is some
I am working on an OSX application using Objective-C, and one of the things
I am working on a project which will include automatically filling out some templates.
In an application scaffolding project on which I'm working, I'm trying to decide whether
I'm working on a I18N application which will be located in Japanese, I don't
I am working on application which processes large amount of text data gathering statistics
I am working on an iPhone application in which I have HTML pages created
I have an word add in application created in c# which is accessing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.