Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using VBA in Word to extract Paragraph Text with formatiing

4,596 views
Skip to first unread message

sippyuconn

unread,
Aug 4, 2007, 10:22:06 AM8/4/07
to
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


Jialiang Ge [MSFT]

unread,
Aug 6, 2007, 4:35:47 AM8/6/07
to
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 (jia...@online.microsoft.com, 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.

Jialiang Ge [MSFT]

unread,
Aug 7, 2007, 9:47:08 PM8/7/07
to
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 (jia...@online.microsoft.com, 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.

sippyuconn

unread,
Aug 10, 2007, 9:38:05 AM8/10/07
to
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]

unread,
Aug 13, 2007, 6:36:24 AM8/13/07
to
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.

==================================================

daniel lie

unread,
Oct 12, 2010, 1:40:15 AM10/12/10
to
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

> On Saturday, August 04, 2007 10:22 AM sippyucon wrote:

> 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

>> Sincerely,
>> Jialiang Ge (jia...@online.microsoft.com, 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 Tuesday, August 07, 2007 9:47 PM jialg wrote:

>>> 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 (jia...@online.microsoft.com, 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.


>>>> 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:


>>>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>>>> Mocking WCF Services Using Moq
>>>>> http://www.eggheadcafe.com/tutorials/aspnet/b76105f1-f832-4b52-82ba-d5a61d435d81/mocking-wcf-services-using-moq.aspx

daniel lie

unread,
Oct 12, 2010, 2:40:01 AM10/12/10
to
It works now. Thank you very much

Submitted via EggHeadCafe - Software Developer Portal of Choice

Silverlight 4 Martin Fractals with WriteableBitmapEx
http://www.eggheadcafe.com/tutorials/aspnet/d312fb6f-9610-449f-a191-26fcede58730/silverlight-4-martin-fractals-with-writeablebitmapex.aspx

0 new messages