紧急!!!请问怎样用VB程序在WORD文档里生成图表?

ZHYPDW 2004-04-20 12:29:09
各位XDJM:
快帮帮忙,请问怎样用VB程序在WORD文档里生成图表?现在已把数据在WORD里生成了表格,如我在WORD文档里有一个6行2列的数据表格,我想用这两列数据生成一个图表,最好是饼形图。要求用VB程序控制。
...全文
138 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZHYPDW 2004-04-28
  • 打赏
  • 举报
回复
UP
ZHYPDW 2004-04-26
  • 打赏
  • 举报
回复
'VB控制Word
'*************************************************************************************************
Dim WordApp As New Word.Application
Dim WordDoc As Word.Document
Dim WordTable As Word.Table
Dim WordCell As Word.Cell
Dim WordIS As Word.InlineShape
Dim strText As String
Dim iTableCell As Integer, iTableRow As Integer
With WordApp
.DisplayAlerts = wdAlertsNone
Set WordDoc = .Documents.Add
WordApp.Selection.ParagraphFormat.Alignment = 1
WordApp.Selection.TypeParagraph
WordApp.Selection.Font.Size = 12
WordApp.Selection.Font.Bold = -1
WordApp.Selection.TypeText "调查报告"
WordApp.Selection.TypeParagraph
WordApp.Selection.TypeParagraph
WordApp.Selection.ParagraphFormat.Alignment = 3
WordApp.Selection.Font.Size = 9
WordApp.Selection.Font.Bold = 0

WordApp.Selection.TypeText "第一层场强"
WordApp.Selection.TypeParagraph
Set WordTable = WordDoc.Tables.Add(WordApp.Selection.Range, 5, 2)
iTableRow = 5: iTableCell = 1
Dim iLoop As Integer, jLoop As Integer
WordTable.Range.Cells(1).Range.Text = "0<=dBuV<26": WordTable.Range.Cells(2).Range.Text = "50"
WordTable.Range.Cells(3).Range.Text = "26<=dBuV<32": WordTable.Range.Cells(4).Range.Text = "241"
WordTable.Range.Cells(5).Range.Text = "32<=dBuV<40": WordTable.Range.Cells(6).Range.Text = "140"
WordTable.Range.Cells(7).Range.Text = "40<=dBuV<95": WordTable.Range.Cells(8).Range.Text = "320"
WordTable.Range.Cells(9).Range.Text = "dBuV<0": WordTable.Range.Cells(10).Range.Text = "13"

WordApp.ActiveDocument.Range(WordTable.Range.Cells(1).Range.Start, WordTable.Range.Cells(10).Range.End).Select '选中要生成图表的数据
WordApp.WordBasic.InsertChart '生成图表

.Visible = True

End With

Set WordCell = Nothing
Set WordDoc = Nothing
Set WordTable = Nothing
Set WordApp = Nothing
'*************************************************************************************************
MsgBox "OK"

以上代码生成一个word文档,有一个5行2列的数据表和一个图表, 现在想问该如何用VB代码去控制生成的图表,比如修改图表的标题等操作。
还有就是现在生成的图表只能是柱形图,为什么不能生成为饼形图?
请各位高人指点指点,谢谢!!!
flyingZFX 2004-04-24
  • 打赏
  • 举报
回复
up
JackDai 2004-04-24
  • 打赏
  • 举报
回复
UP
ZHYPDW 2004-04-21
  • 打赏
  • 举报
回复
有哪位好心人可以把具体实现的代码贴出来吗?真的很急!!!!!!!!!!
ZHYPDW 2004-04-21
  • 打赏
  • 举报
回复
'VB控制Word
'*************************************************************************************************
Dim WordApp As New Word.Application
Dim WordDoc As Word.Document
Dim WordTable As Word.Table
Dim WordCell As Word.Cell
Dim WordIS As Word.InlineShape
Dim strText As String
Dim iTableCell As Integer, iTableRow As Integer
With WordApp
.DisplayAlerts = wdAlertsNone
Set WordDoc = .Documents.Add
WordApp.Selection.ParagraphFormat.Alignment = 1
WordApp.Selection.TypeParagraph
WordApp.Selection.Font.Size = 12
WordApp.Selection.Font.Bold = -1
WordApp.Selection.TypeText "调查报告"
WordApp.Selection.TypeParagraph
WordApp.Selection.TypeParagraph
WordApp.Selection.ParagraphFormat.Alignment = 3
WordApp.Selection.Font.Size = 9
WordApp.Selection.Font.Bold = 0
Set WordTable = WordDoc.Tables.Add(WordApp.Selection.Range, 5, 2)
iTableRow = 5: iTableCell = 1
Dim iLoop As Integer, jLoop As Integer
WordTable.Range.Cells(1).Range.Text = "0<=dBuV<26": WordTable.Range.Cells(2).Range.Text = "0"
WordTable.Range.Cells(3).Range.Text = "26<=dBuV<32": WordTable.Range.Cells(4).Range.Text = "41"
WordTable.Range.Cells(5).Range.Text = "32<=dBuV<40": WordTable.Range.Cells(6).Range.Text = "140"
WordTable.Range.Cells(7).Range.Text = "40<=dBuV<95": WordTable.Range.Cells(8).Range.Text = "420"
WordTable.Range.Cells(9).Range.Text = "dBuV<0": WordTable.Range.Cells(10).Range.Text = "13"
WordApp.Selection.Move 5, iTableRow

Set WordIS = WordApp.Selection.InlineShapes.AddOLEObject("Excel.Chart.8", "", False, False) '("Excel.Chart.8", "", False, False)
'**********************************************************************************************************

WordApp.Selection.Move 5, 1
WordApp.Selection.TypeParagraph
WordApp.Selection.TypeText "第二层场强"
WordApp.Selection.TypeParagraph

.Visible = True

End With
Set WordApp = Nothing
Set WordDoc = Nothing
Set WordTable = Nothing
Set WordCell = Nothing
'*************************************************************************************************
MsgBox "OK"


以上代码生成一个word文档,有一个5行2列的数据表和一个图表(图表数据是自动生成的默认值),现在不知该如何用代码更改图表的数据,是其和上面的5行2列的数据表的数据一致。
请各位高人指点指点,谢谢!!!
射天狼 2004-04-20
  • 打赏
  • 举报
回复
可以在WORD中插入EXCEL控制,在EXCEL中可以输入饼图!!
具体的操作可以上WORD和EXCEL中录制宏看一下就知道了!!
ryuginka 2004-04-20
  • 打赏
  • 举报
回复
up
ZHYPDW 2004-04-20
  • 打赏
  • 举报
回复
在WORD里录制的宏是:
Selection.MoveUp Unit:=wdLine, Count:=11
Selection.MoveLeft Unit:=wdCharacter, Count:=22
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=6, Extend:=wdExtend
WordBasic.InsertChart

我现在就是不明白在VB里怎样选定两列数据,不知VB的代码怎么写?
EXCEL里我可以用VB生成图表,但现在我不想用到EXCEL,只需要单单控制WORD就可以了。
再次谢谢你的回复!!!
射天狼 2004-04-20
  • 打赏
  • 举报
回复
你怎么控制的,我没有详细的代码,现在工作,没有时间弄!!

工程->引用->Microsoft Word 9.0 Object Library
工程->引用->Microsoft Excel 9.0 Object Library(后面为版本号)

dim wd as New Word.Application
dim exl as New Excel.Application

把录制的宏用这两个对象执行就可以了,你的代码怎么写的!?
ZHYPDW 2004-04-20
  • 打赏
  • 举报
回复
to :cuizm(射天狼)
谢谢你的回复。我也在WORD录制了宏,但如果在VB里实现好像不是很好控制,如你能把详细的VB代码贴出来就好了。

7,786

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧