在EXCEL里复制图表插入到WORD里怎么样用VB实现? 20
可能是我没表达清楚,就是在EXCEL里写一个宏,能够生成一个Word同时还能把EXCEL里的图表插入到Word里,怎样用宏来实现?...
可能是我没表达清楚,就是在EXCEL里写一个宏,能够生成一个Word同时还能把EXCEL里的图表插入到Word里,怎样用宏来实现?
展开
2个回答
展开全部
完全可以。以下是利用excel自动插入到word中,生成格式规范的简报。相信这对你一定有帮助 。
如果有什么问题,请回复邮箱:xuyong20040121@163.com
Sub Wordnew3()
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim objrange As Word.Range
Dim rtitle As String '简报红头
Dim otitle As String '简报期数
Dim btitle As String '印制机关
Dim ltitle As String '发文日期
Dim ttitle As String '表格标题
On Error GoTo errHandle
Sheet1.Select
rtitle = Cells(1, 1)
otitle = Cells(2, 1)
btitle = Cells(3, 1)
'ltitle = Cells(3, 4)
ltitle = CStr(Format(Cells(3, 4), "YYYY")) & "年" & CStr(Format(Cells(3, 4), "M")) & "月" & CStr(Format(Cells(3, 4), "DD")) & "日"
ttitle = Cells(5, 1)
Range(Cells(6, 1), Cells(28, 7)).Select
Selection.Copy
Set objWordApp = New Word.Application
strtemp = ThisWorkbook.Path & "\" & "hgzb.dot" '模板相对位置
Set objWord = objWordApp.Documents.Add(Template:=strtemp, NewTemplate:=False, DocumentType:=0) '根据模板新建文档
objWord.Application.Visible = True
objWord.Shapes(3).TextFrame.TextRange = rtitle
objWord.Shapes(4).TextFrame.TextRange = btitle
objWord.Shapes(5).TextFrame.TextRange = otitle
objWord.Shapes(6).TextFrame.TextRange = ltitle
objWord.Paragraphs(9).Range.InsertAfter (ttitle)
objWord.Paragraphs(10).Range.Select
objWord.Application.Selection.Style = objWord.Styles("表题")
'输入表格
objWord.Paragraphs(11).Range.Select
objWordApp.Selection.PasteExcelTable False, False, False
'输入一个段落标记
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.InsertAfter (ttitle)
'为了方便,再次输入一次同样的表格标题
objWord.Application.Selection.Style = objWord.Styles("表题")
objWord.Application.Selection.InsertAfter Text:=vbCrLf
objWord.Application.Selection.Paragraphs(1).Range.Select
objWord.Application.Selection.MoveDown unit:=wdParagraph, Count:=1
'同上,再次输入同样的表格
objWordApp.Selection.PasteExcelTable False, False, False
'对所有表格格式化
'判断有无表格,如果有就处理
If objWord.Tables.Count >= 1 Then
Dim atable As Word.Table
For Each atable In objWord.Tables
atable.Select
'设置表格的字体、段落
objWord.Application.Selection.Font.Size = 10
With objWord.Application.Selection.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.LineUnitBefore = 0
.LineUnitAfter = 0
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = True
.WordWrap = True
End With
With atable
.Rows.Height = CentimetersToPoints(0.4)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.09)
.RightPadding = CentimetersToPoints(0.09)
.AutoFitBehavior (wdAutoFitWindow)
End With
'以下设置表格线
With atable
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
End With
Next atable
End If
errExit:
Set objsel = Nothing
Set objWord = Nothing
Set objWordApp = Nothing
Exit Sub
errHandle:
MsgBox Err.Description
Resume errExit
End Sub
http://zhidao.baidu.com/question/158919791.html?fr=uc_push
如果有什么问题,请回复邮箱:xuyong20040121@163.com
Sub Wordnew3()
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim objrange As Word.Range
Dim rtitle As String '简报红头
Dim otitle As String '简报期数
Dim btitle As String '印制机关
Dim ltitle As String '发文日期
Dim ttitle As String '表格标题
On Error GoTo errHandle
Sheet1.Select
rtitle = Cells(1, 1)
otitle = Cells(2, 1)
btitle = Cells(3, 1)
'ltitle = Cells(3, 4)
ltitle = CStr(Format(Cells(3, 4), "YYYY")) & "年" & CStr(Format(Cells(3, 4), "M")) & "月" & CStr(Format(Cells(3, 4), "DD")) & "日"
ttitle = Cells(5, 1)
Range(Cells(6, 1), Cells(28, 7)).Select
Selection.Copy
Set objWordApp = New Word.Application
strtemp = ThisWorkbook.Path & "\" & "hgzb.dot" '模板相对位置
Set objWord = objWordApp.Documents.Add(Template:=strtemp, NewTemplate:=False, DocumentType:=0) '根据模板新建文档
objWord.Application.Visible = True
objWord.Shapes(3).TextFrame.TextRange = rtitle
objWord.Shapes(4).TextFrame.TextRange = btitle
objWord.Shapes(5).TextFrame.TextRange = otitle
objWord.Shapes(6).TextFrame.TextRange = ltitle
objWord.Paragraphs(9).Range.InsertAfter (ttitle)
objWord.Paragraphs(10).Range.Select
objWord.Application.Selection.Style = objWord.Styles("表题")
'输入表格
objWord.Paragraphs(11).Range.Select
objWordApp.Selection.PasteExcelTable False, False, False
'输入一个段落标记
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.InsertAfter (ttitle)
'为了方便,再次输入一次同样的表格标题
objWord.Application.Selection.Style = objWord.Styles("表题")
objWord.Application.Selection.InsertAfter Text:=vbCrLf
objWord.Application.Selection.Paragraphs(1).Range.Select
objWord.Application.Selection.MoveDown unit:=wdParagraph, Count:=1
'同上,再次输入同样的表格
objWordApp.Selection.PasteExcelTable False, False, False
'对所有表格格式化
'判断有无表格,如果有就处理
If objWord.Tables.Count >= 1 Then
Dim atable As Word.Table
For Each atable In objWord.Tables
atable.Select
'设置表格的字体、段落
objWord.Application.Selection.Font.Size = 10
With objWord.Application.Selection.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 12
.LineUnitBefore = 0
.LineUnitAfter = 0
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = True
.WordWrap = True
End With
With atable
.Rows.Height = CentimetersToPoints(0.4)
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.09)
.RightPadding = CentimetersToPoints(0.09)
.AutoFitBehavior (wdAutoFitWindow)
End With
'以下设置表格线
With atable
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorBlack
End With
End With
Next atable
End If
errExit:
Set objsel = Nothing
Set objWord = Nothing
Set objWordApp = Nothing
Exit Sub
errHandle:
MsgBox Err.Description
Resume errExit
End Sub
http://zhidao.baidu.com/question/158919791.html?fr=uc_push
展开全部
好像不行耶!
要知道在VB中doc和xls文件只能调用word和excel来打开
VB是不能独立读取这些文件的。
除非你对doc和xls文件格式有相当了解
达到了wps2005工程师的水平
xls文件到还能通过其他方法来获取其中的数据
而且只能获得数据,
格式信息一定要通过引用office对象来取得
doc文件就更不要多想了
肯定不行。
但后台打开文件不让软件界面显示是完全可以做到的。
事实是
当你引用了office对象
除非专门加入Visible = True 命令显示word或excel界面
不然软件的界面是不会显示的。
要知道在VB中doc和xls文件只能调用word和excel来打开
VB是不能独立读取这些文件的。
除非你对doc和xls文件格式有相当了解
达到了wps2005工程师的水平
xls文件到还能通过其他方法来获取其中的数据
而且只能获得数据,
格式信息一定要通过引用office对象来取得
doc文件就更不要多想了
肯定不行。
但后台打开文件不让软件界面显示是完全可以做到的。
事实是
当你引用了office对象
除非专门加入Visible = True 命令显示word或excel界面
不然软件的界面是不会显示的。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询