工具箱中的MS Word控件 [英] MS Word Control In Toolbox

查看:55
本文介绍了工具箱中的MS Word控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我想在我的表单应用程序中使用MS Word。

如何在上面放置MS Word Control Icon工具箱???



任何人都可以帮助我!



我将非常感谢这一行为那种。





谢谢。

Hi !

I want to use MS Word in my form application.
How can i place MS Word Control Icon on the Toolbox ???

Can anyone help me !

I will be very grateful for this act of kind.


Thanks.

推荐答案

试试这个...... 。

本文中的示例代码演示了如何执行以下操作:

插入带有文本和格式的段落。

浏览和修改各种范围在文档中。

插入表格,格式化表格,并用数据填充表格。

添加图表。

创建新Word通过使用Visual Basic .NET中的自动化文档,请按照下列步骤操作:

启动Microsoft Visual Studio .NET。在文件菜单上,单击新建,然后单击项目。在项目类型下单击Visual Basic项目,然后单击模板下的Windows应用程序。 Form1是默认创建的。

添加对Microsoft Word对象库的引用。为此,请按照下列步骤操作:

在项目菜单上,单击添加引用。

在COM选项卡上,找到Microsoft Word对象库,然后单击选择。 />


注意Microsoft Office 2003及更高版本的Office包括主互操作程序集(PIA)。 Microsoft Office XP不包含PIA,但可以下载它们。有关Office XP PIA的更多信息,请单击下面的文章编号,以查看Microsoft知识库中相应的文章:

328912 Microsoft Office XP主互操作程序集(PIA)可供下载

在添加引用对话框中单击确定以接受您的选择。如果收到为所选库生成包装器的提示,请单击是。

在视图菜单上,选择工具箱以显示工具箱,然后向Form1添加一个按钮。

双击Button1。将出现表单的代码窗口。

在代码窗口中,替换以下代码

Private Sub Button1_Click(ByVal sender As System.Object,_

ByVal e As System.EventArgs)处理Button1.Click



End Sub

with:

Private Sub Button1_Click(ByVal sender As System.Object,_

ByVal e As System.EventArgs)Handles Button1.Click



Dim oWord As Word.Application

Dim oDoc As Word.Document

Dim oTable As Word.Table

Dim oPara1 As Word.Paragraph,oPara2 As Word。段落

Dim oPara3 As Word.Paragraph,oPara4 As Word.Paragraph

Dim oRng As Word.Range

Dim oShape As Word.InlineShape

Dim oChart As Object

Dim Pos As Double



'启动Word并打开文档模板。 />
oWord = CreateObject(Word.Application)

oWord.Visible = True

oDoc = oWord.Documents.Add



'在文件开头插入一个段落。

oPara1 = oDoc.Content.Paragraphs.Add

oPara1.Range.Text =Heading 1

oPara1.Range.Font.Bold = True

oPara1.Format.SpaceAfter = 24 '24 pt后段距。

oPara1.Range。 InsertParagraphAfter()



'在文档的末尾插入一个段落。

'** \ endofdoc是预定义的书签。

oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item(\ endofdoc)。范围)

oPara2.Range.Text =Heading 2

oPara2.Format.SpaceAfter = 6

oPara2.Range.InsertParagraphAfter()



'插入另一段。

oPara3 = oDoc .Content.Paragraphs.Add(oDoc.Bookmarks.Item(\ endofdoc)。Range)

oPara3.Range.Text =这是普通文本的句子。现在这里有一张桌子:

oPara3.Range.Font.Bold = False

oPara3.Format.SpaceAfter = 24

oPara3.Range .InsertParagraphAfter()



'插入3 x 5表,用数据填充,并使第一行

'粗体和斜体。

Dim r As Integer,c As Integer

oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item(\ endofdoc)。Range,3, 5)

oTable.Range.ParagraphFormat.SpaceAfter = 6

对于r = 1到3

对于c = 1到5
oTable.Cell(r,c).Range.Text =r&r&c&c

下一页

下一页
oTable.Rows.Item(1).Range.Font.Bold = True

oTable.Rows.Item(1).Range.Font.Italic = True



'在桌子后添加一些文字。

'oTable.Range.InsertParagraphAfter()

oPara4 = oD oc.Content.Paragraphs.Add(oDoc.Bookmarks.Item(\ endofdoc)。范围)

oPara4.Range.InsertParagraphBefore()

oPara4.Range。 Text =这是另一张桌子:

oPara4.Format.SpaceAfter = 24

oPara4.Range.InsertParagraphAfter()



'插入一个5 x 2表,用数据填充,然后更改列宽。

oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item(\ endofdoc )。范围,5,2)

oTable.Range.ParagraphFormat.SpaceAfter = 6

对于r = 1到5

对于c = 1到2

oTable.Cell(r,c).Range.Text =r&r&c&c

下一页

下一页

oTable.Columns.Item(1).Width = oWord.InchesToPoints(2)'更改第1列和第2列的宽度

oTable.Columns.Item (2).Width = oWord.InchesToPoints(3)



'继续插入文字。当您从

'文档的顶部到达7英寸时,插入一个硬分页符。

Pos = oWord.InchesToPoints(7)

oDoc.Bookmarks.Item(\ endofdoc)。Range.InsertParagraphAfter()

Do

oRng = oDoc.Bookmarks.Item(\ endofdoc) .Range

oRng.ParagraphFormat.SpaceAfter = 6

oRng.InsertAfter(一行文字)

oRng.InsertParagraphAfter()

Loop While Pos> = oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)

oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

oRng .InsertBreak(Word.WdBreakType.wdPageBreak)

oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

oRng.InsertAfter(我们现在在第2页。这是我的图表:)

oRng.InsertParagraphAfter()



'插入图表,更改图表。

oShape = oDoc.Bookmarks.Item(\ endofdoc)。Range.InlineShapes.AddOLEObject(_

ClassType:=MSGraph.Chart。 8,FileName _

:=,LinkToFile:= False,DisplayAsIcon:= False)

oChart = oShape.OLEFormat.Object

oChart.charttype = 4'xlLine = 4

oChart.Application.Update()

oChart.Application.Quit()

'如果希望你可以在这里使用oChart对象上的Microsoft Graph

'对象模型继续对

'图表进行更改。

oShape.Width = oWord.InchesToPoints(6.25)

oShape.Height = oWord.InchesToPoints(3.57)



'之后添加文字图表。

oRng = oDoc.Bookmarks.Item(\ endofdoc)。范围

oRng.InsertParagraphAfter()

oRng.InsertAfter (E ND。)



'全部完成。关闭此表格。

Me.Close()



End Sub

将以下代码添加到顶部of Form1.vb:

导入Word = Microsoft.Office.Interop.Word

按F5构建并运行程序。

之后代码完成后,检查为您创建的文档。该文档包含两页格式化的段落,表格和图表。
Try this....
The sample code in this article demonstrates how to do the following:
Insert paragraphs with text and formatting.
Browse and modify various ranges within a document.
Insert tables, format tables, and populate the tables with data.
Add a chart.
To create a new Word document by using Automation from Visual Basic .NET, follow these steps:
Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic Projects, then click Windows Application under Templates. Form1 is created by default.
Add a reference to the Microsoft Word Object Library. To do this, follow these steps:
On the Project menu, click Add Reference.
On the COM tab, locate the Microsoft Word Object Library and click Select.

Note Microsoft Office 2003 and later versions of Office include Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they may be downloaded. For more information about Office XP PIAs, click the following article number to view the article in the Microsoft Knowledge Base:
328912 Microsoft Office XP primary interop assemblies (PIAs) are available for download
Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click Yes.
On the View menu, select Toolbox to display the Toolbox, and then add a button to Form1.
Double-click Button1. The code window for the form appears.
In the code window, replace the following code
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

End Sub
with:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
Dim oRng As Word.Range
Dim oShape As Word.InlineShape
Dim oChart As Object
Dim Pos As Double

'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add

'Insert a paragraph at the beginning of the document.
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "Heading 1"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()

'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagraphAfter()

'Insert another paragraph.
oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
oPara3.Range.Font.Bold = False
oPara3.Format.SpaceAfter = 24
oPara3.Range.InsertParagraphAfter()

'Insert a 3 x 5 table, fill it with data, and make the first row
'bold and italic.
Dim r As Integer, c As Integer
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 3
For c = 1 To 5
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Rows.Item(1).Range.Font.Bold = True
oTable.Rows.Item(1).Range.Font.Italic = True

'Add some text after the table.
'oTable.Range.InsertParagraphAfter()
oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara4.Range.InsertParagraphBefore()
oPara4.Range.Text = "And here's another table:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagraphAfter()

'Insert a 5 x 2 table, fill it with data, and change the column widths.
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 5, 2)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 5
For c = 1 To 2
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Columns.Item(1).Width = oWord.InchesToPoints(2) 'Change width of columns 1 & 2
oTable.Columns.Item(2).Width = oWord.InchesToPoints(3)

'Keep inserting text. When you get to 7 inches from top of the
'document, insert a hard page break.
Pos = oWord.InchesToPoints(7)
oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()
Do
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.ParagraphFormat.SpaceAfter = 6
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter()
Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertAfter("We're now on page 2. Here's my chart:")
oRng.InsertParagraphAfter()

'Insert a chart and change the chart.
oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
ClassType:="MSGraph.Chart.8", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
oChart = oShape.OLEFormat.Object
oChart.charttype = 4 'xlLine = 4
oChart.Application.Update()
oChart.Application.Quit()
'If desired, you can proceed from here using the Microsoft Graph
'Object model on the oChart object to make additional changes to the
'chart.
oShape.Width = oWord.InchesToPoints(6.25)
oShape.Height = oWord.InchesToPoints(3.57)

'Add text after the chart.
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.InsertParagraphAfter()
oRng.InsertAfter("THE END.")

'All done. Close this form.
Me.Close()

End Sub
Add the following code to the top of Form1.vb:
Imports Word = Microsoft.Office.Interop.Word
Press F5 to build and run the program.
After the code completes, examine the document that is created for you. The document contains two pages of formatted paragraphs, tables, and a chart.


这篇关于工具箱中的MS Word控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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