Copy MS Word Paragraph to RichTextBox with formatting

G

Guest

Hi

Is there a way thru automation to extract a paragraph from a word document
and put into a RichTextBox with the original Word formatting.

Roght now I only get the plain text from para.Range.Text

Thanks
 
J

Jialiang Ge [MSFT]

Hello sippyuconn,

From your post, my understanding on this issue is: you want to copy a
certain paragraph of a word document to a rich text box. If I'm off base,
please feel free to let me know.

I think you may use the Copy method to copy the paragraph into clipboard
first, then paste it into the RichTextBox control. Please refer to the
following sample code which is for a winform project.

Dim app As Word.Application
app = CreateObject("Word.Application")
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

Dim doc As Word.Document
doc = app.Documents.Open("d:\test.docx")
doc.Paragraphs(1).Range.Copy()
Me.RichTextBox1.Paste()
doc.Close()
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsAll

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi

That is what I need - one more question

If I do this process using VBA thru Word it takes about 10 seconds

If I run this code thru DotNet C# calling word it takes 15 minutes to process
30,000 paragraphs. Is the way I am doing this in DotNet correct or is there a
faster way???

Thanks

ApplicationClass WordApp;

public WordDoc(string sFileName)
{
WordApp = new ApplicationClass();
WordApp.Visible = false;
FileName = sFileName;
}

public void Process()
{


object read_only = false;
object visible = true;
object ofalse = false;
object otrue = true;
object dynamic = 2;
object missing = System.Reflection.Missing.Value;
object oFileName = FileName;

Document Doc = WordApp.Documents.Open(ref oFileName, ref
missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref
missing, ref missing,
ref missing, ref missing, ref missing, ref
missing, ref missing);


for(int i = 1; i <= Doc.Paragraphs.Count; i++)
{
string sLine = Doc.Paragraphs.Range.Text;


.....

}
 
J

Jialiang Ge [MSFT]

Hello sippyuconn

Yes, the way you are doing this in .Net is correct.

The performance of a ".NET-COM interop" application does lag behind VBA and
COM approaches. There are several factors that influence the performance:
1. The .net start up cost is inherently expensive. Applications written
with .net must incur the overhead of JIT compilation. JIT compilation is a
necessary evil and cannot be avoided. However, steps may be taken to reduce
the performance expense of JIT compilation. One approach is to use the
NGEN.exe which forces a JIT compilation of all the methods in the assembly.
Since the methods are Jitted, JIT compilation is no longer necessary when
the application first starts up.
2. Another performance factor influencing the process time is the expense
of calling through the thick layers of automation skin that wrap the
Microsoft office com objects. VBA, built and optimized to interact with
Microsoft office, has a shorter distance to travel than .NET.

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hi sippyuconn,

Would you mind letting me know the result of the suggestions? If you need
further assistance, feel free to let me know. I will be more than happy to
be of assistance.

Have a great day!

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top