我想要一个将逐段显示doc字的代码 [英] i want a code which will display the word doc paragraph by paragraph

查看:92
本文介绍了我想要一个将逐段显示doc字的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试在文本框中逐段显示Word文档的内容.

我尝试了一次显示所有内容并显示最后一段的代码.

但我想手动中断显示内容.

例如,如果我单击下一个按钮,则应显示下一个段落,如果我单击上一个按钮,则应显示上一个段落.

请给我建议一个代码.

正在尝试此代码.

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.

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


我正在尝试此代码,但显示的是最后一段.

当我将代码TextBox.Text = p.range.text更改为messagebox.show(p.range.text)...时,它正在逐一显示,但是我想要在文本框中输入相同的类型,当我单击下一步"按钮时,它应该转到下一段.

请把代码放给我.

在此先谢谢您


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

推荐答案

您应该删除循环并仅在处理单击
事件后增加或减少p 下一个和上一个按钮?

我确定有一种方法可以将p作为数组访问您感兴趣的正确段落.

查看p是变量的种类以及如何声明它以及如何访问它.

然后,您应该输入以下内容:

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:

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


在用于按下NEXT按钮的事件处理程序中.


In the event handler for pressing the NEXT button.

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


在用于按下PREVIOUS按钮的事件处理程序中.

注意数组",p的列表或类型中允许的最小值和最大值.

如果没有办法以受控的方式遍历p(我对此表示严重怀疑),那么您当然可以按照将每个段落放入一个手工数组中的方式进行操作.然后,您可以在代码中使用该数组表示您想要的内容.

这不是代码,而是一些有关如何解决此问题的指针.

祝你好运.


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.


琼·默特,谢谢你的评论.升级后我的回答是:

如果要将每个段落加载到文本框中,您的代码可以,但是您需要知道TextBox1仅显示最后一个段落文本,因为在循环的每个步骤中,TextBox1的文本都将被覆盖.您需要设置Multiline = True然后:
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:
'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



如果要使用NEXTPREVIOUS按钮逐步加载数据,则需要知道集合中的每个段落都有其索引,因此,正如Joan Murt所写的那样,您可以遍历段落的集合. 不必将段落加载到List,Array或任何其他面向数组"的对象中,因为它本身就是集合(列表,数组)!



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!

'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


在上面的示例中,必须在模块顶部将i变量声明为该模块中的全局变量.


In the above example i variable must declared in the top of module as a global variable in this module.


这篇关于我想要一个将逐段显示doc字的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆