Using VBA in Word to extract Paragraph Text with formatiing

S

sippyuconn

Hi

I am looping thru a Word document and extracting certain Paragraphs

Is there anyway I can save this text with the Formating????
Some of the Paragraph text maybe bold or different coloring that I want to
save
to display later

Is there a way to copy this paragraph to a new word document or copy into a
vba or DotNet control and save as RTF or HTML ????

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 new word document with formatting
or copy into a vba/DotNet control and save as RTF or HTML. If I'm off base,
please feel free to let me know.

To copy a certain paragraph of a word document to a new word document with
formatting, you may use the Copy and Paste methods. Please refer to the
following sample code.

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim targDoc As Word.Document
On Error Resume Next
Set targDoc = Application.Documents.Open("d:\test.docx")
On Error GoTo 0

ThisDocument.Paragraphs(1).Range.Copy
targDoc.Select
Application.Selection.Paste
targDoc.Save
targDoc.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

If you want to save the paragraph as other format, such as RTF or HTML, you
may use targDoc.SaveAs method
(http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.word.docume
nt.saveas(VS.80).aspx) and specify the corresponding file format: the
RTF's FileFormat is wdFormatRTF and HTML's is wdFormatHTML.

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.
 
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.
 
S

sippyuconn

Hi

Yes that seems to help. Here is more explaination of what I am doing

I am examining paragraphs then picking out certain ones and trying to save
the original formatting to display latter in a RTFBox or HTML control in VB.
So I find each Chapter below 1 and 2 and try to save the following lines with
any format they have like bold/color/italic etc


Chapter1
----------
<bold> Some Text<\bold> is here to <italic> to copy <\italic>

Chapter2
----------
<bold> Some Text<\bold> is here to <italic> to copy <\italic>

When I use you method I get a new document created with alot of formatting.
Color tables etc that word puts there.
In my case I look for sections of a document then I want just the RTF or
HTML that describes thes formatting of just the line or lines I have copied

Thanks
 
J

Jialiang Ge [MSFT]

Hello sippyuconn,

Do you mean to convert a piece of Word with formatting to the corresponding
HTML or RTF form? For instance,
Suppose the following sentence in Word:
'How to convert [bold]Word[/bold] to Html'
Do you mean to get a string 'How to convert <B>Word</B> to Html" with HTML
tag?

Sorry, VBA does not provide such function to parse a part of text in Word
to the HTML or RTF format directly. The workaround is to save that part of
Word to Html or RTF document first with SaveAs method (see my first reply),
then extract the content you need.

Sincerely,
Jialiang Ge
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.
 
D

daniel lie

Hi Jialiang,

I am trying to extract paragraphs from one word to another word document with formatting. I saw your code that you wrote to sippyuconn and tried it out and it actually works for me.

However, I would like to be able to extract multiple paragraphs and copy them into the target document. How do you suppose I do?

here is your code that I modified

For i = 1 To ThisDocument.Paragraphs.Count
ThisDocument.Paragraphs(i).Range.Copy
targetDoc.Select
Application.Selection.Paste

' move to the next line and insert the texts

Next i

targetDoc.Save
targetDoc.Close

Thank you
Hi

I am looping thru a Word document and extracting certain Paragraphs

Is there anyway I can save this text with the Formating????
Some of the Paragraph text maybe bold or different coloring that I want to
save
to display later

Is there a way to copy this paragraph to a new word document or copy into a
vba or DotNet control and save as RTF or HTML ????

Thanks
On Monday, August 06, 2007 4:35 AM jialg wrote:
Hello sippyuconn,

From your post, my understanding on this issue is: you want to copy a
certain paragraph of a word document to a new word document with formatting
or copy into a vba/DotNet control and save as RTF or HTML. If I'm off base,
please feel free to let me know.

To copy a certain paragraph of a word document to a new word document with
formatting, you may use the Copy and Paste methods. Please refer to the
following sample code.

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim targDoc As Word.Document
On Error Resume Next
Set targDoc = Application.Documents.Open("d:\test.docx")
On Error GoTo 0

ThisDocument.Paragraphs(1).Range.Copy
targDoc.Select
Application.Selection.Paste
targDoc.Save
targDoc.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

If you want to save the paragraph as other format, such as RTF or HTML, you
may use targDoc.SaveAs method
(http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.word.docume
nt.saveas(VS.80).aspx) and specify the corresponding file format: the
RTF's FileFormat is wdFormatRTF and HTML's is wdFormatHTML.

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.
On Friday, August 10, 2007 9:38 AM sippyucon wrote:
Hi

Yes that seems to help. Here is more explaination of what I am doing

I am examining paragraphs then picking out certain ones and trying to save
the original formatting to display latter in a RTFBox or HTML control in VB.
So I find each Chapter below 1 and 2 and try to save the following lines with
any format they have like bold/color/italic etc


Chapter1
----------
<bold> Some Text<\bold> is here to <italic> to copy <\italic>

Chapter2
----------
<bold> Some Text<\bold> is here to <italic> to copy <\italic>

When I use you method I get a new document created with alot of formatting.
Color tables etc that word puts there.
In my case I look for sections of a document then I want just the RTF or
HTML that describes thes formatting of just the line or lines I have copied

Thanks


"Jialiang Ge [MSFT]" wrote:
On Monday, August 13, 2007 6:36 AM jialg wrote:
Hello sippyuconn,

Do you mean to convert a piece of Word with formatting to the corresponding
HTML or RTF form? For instance,
Suppose the following sentence in Word:
'How to convert [bold]Word[/bold] to Html'
Do you mean to get a string 'How to convert <B>Word</B> to Html" with HTML
tag?

Sorry, VBA does not provide such function to parse a part of text in Word
to the HTML or RTF format directly. The workaround is to save that part of
Word to Html or RTF document first with SaveAs method (see my first reply),
then extract the content you need.

Sincerely,
Jialiang Ge
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