Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi
I'm trying to display the contents of word document paragraph by paragraph in a textbox.

I tried a code that is displaying everything at a time and displaying the last paragraph.

But i want to display the contents with manual interruption.

For example if i click on next button it should display the next paragraph and if i click on previous button it should display the previous paragraph.

Please suggest me a code.

Am trying this code.
VB
For Each p In currentDocument.Paragraphs
  TextBox.Text = p.range.text
Next p

I'm trying this code but it is displaying the last paragraph.

When I change the code TextBox.Text = p.range.text into messagebox.show(p.range.text)... It is displaying one by one but i want the same type in textbox and when i click on next button it should go to the next paragraph.

Please drop me the code.

Thanks in advance
Posted
Updated 29-Aug-12 4:32am
v2
Comments
[no name] 29-Aug-12 10:33am    
Repost
Joan M 29-Aug-12 10:55am    
Repost...

Shouldn't you remove the loop and increase or decrease p only after handling the event of clicking
the buttons NEXT and PREVIOUS?

I'm sure there is a way to access p as an array accessing the right paragraph you are interested in.

See the kind of variable that p is and how it is declared and how you can access it.

Then you should put something like:

VB
i = i + 1
TextBox.Text = p[i].range.text

In the event handler for pressing the NEXT button.

VB
i = i - 1
TextBox.Text = p[i].range.text

In the event handler for pressing the PREVIOUS button.

Be careful with the minimum and maximum values allowed in the "array", list or type of p.

If there is no way to iterate in a controlled way through p (I seriously doubt it) you could of course do what you are doing putting each paragraph inside a handmade array. Then you could use that array in your code to represent what you want.

This is not the code, but a few pointers on how I would approach the issue.

Good luck.
 
Share this answer
 
Comments
Maciej Los 29-Aug-12 11:20am    
Comment from OP:
if i use the above it is throwing an error.
The libraries i declared are:

Imports System
Imports System.IO
Imports Microsoft.Office.Interop


The variables i declared are:
Dim p
Dim currentDocument
Joan M 29-Aug-12 11:25am    
Probably you are confused but the OP has not posted that here...
Who knows, probably, as this is a repost, you've seen the other one and then you've posted this...
Whatever you are wrong.
PS: Dim P is not clear enough to know if you can iterate this type or not.
Maciej Los 29-Aug-12 11:31am    
Maybe i'm wrong, maybe not. It was only one solution, so... "if i use the above..." is probably the comment to your answer.
Joan M 29-Aug-12 11:35am    
o_O¡
Maciej Los 29-Aug-12 11:51am    
OP don't need to load paragraphs into array. Word document contains a collection of pragraphs, so OP can iterate through the collection of paragraphs, using: For Each p In CollectionOfParagraphs ... Next.
Joan Murt, thank you for your comments. My answer after upgrade is:

If you would like to load each paragraphs in to textbox, your code is OK, but you need to know that TextBox1 displays only last paragraph text, because in each step of loop the text of TextBox1 is overwriting. You need to set Multiline = True and then:
VB
'Probably the correct declaration should looks like:
Dim p As Word.Paragraph 
Dim currentDocument AS Word.Document

For Each p In currentDocument.Paragraphs
  TextBox.Text = TextBox.Text + p.range.text
Next p


If you would like to load data step by step using NEXT and PREVIOUS button, you need to know that each paragraph in the collection has its index, so, as Joan Murt wrote, you can move through the collection of paragraphs. It's not necessary to load paragraphs into List, Array or any other "array-oriented" object, because it's the collection (list, array) itself!
VB
'the body of NEXT button click
i += 1
'the body of PREVIOUS button click
i -= 1
'then
p = doc.Paragraphs(i)
TextBox1.Text = p.Range.Text

In the above example i variable must declared in the top of module as a global variable in this module.
 
Share this answer
 
v4
Comments
Maciej Los 29-Aug-12 11:38am    
@NoName, thank you for vote of 1 without any explanations.
vinay597 30-Aug-12 7:21am    
In the same code is there any logic to display the paragraphs from a particular point....i mean i want to delete all the paragraphs before it.
Maciej Los 1-Sep-12 7:10am    
What you mean: "from the particular point"? Do you want to start from n. paragraph, not from begining one? Why do you want to delete all paragraphs?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900