注册 登录
编程论坛 VB6论坛

请教大家以下,我写生成word文档,里面生成了表格,表格里的内容都赋值了,现在要在表格下方写其他内容,怎么写都是在表格第1行里。

dushuzai 发布于 2017-11-24 22:49, 1360 次点击
Dim myword As Word.Application
 Dim myd As Word.Document
 Dim myr As Word.Range
 Dim myr1 As Word.Range
 Dim mys As Word.Selection
 Dim myt As Word.Table

myword.Selection.EndKey unit:=wdStory '***************表格光标定位
Call myword.ActiveDocument.Tables.Add(myword.Application.Selection.Range, 16, 1, 1, 0) '***************建立表格



myword.ActiveDocument.Tables(1).Cell(1, 1).Range.InsertAfter "1"  '访问表格
myword.ActiveDocument.Tables(1).Cell(2, 1).Range.InsertAfter "2"
myword.ActiveDocument.Tables(1).Cell(3, 1).Range.InsertAfter "3"
myword.ActiveDocument.Tables(1).Cell(4, 1).Range.InsertAfter "4"
myword.ActiveDocument.Tables(1).Cell(5, 1).Range.InsertAfter "5"
myword.ActiveDocument.Tables(1).Cell(6, 1).Range.InsertAfter "6"
myword.ActiveDocument.Tables(1).Cell(7, 1).Range.InsertAfter "7"
myword.ActiveDocument.Tables(1).Cell(8, 1).Range.InsertAfter "8"
myword.ActiveDocument.Tables(1).Cell(9, 1).Range.InsertAfter "9"
myword.ActiveDocument.Tables(1).Cell(10, 1).Range.InsertAfter "10 "
myword.ActiveDocument.Tables(1).Cell(11, 1).Range.InsertAfter "11"
myword.ActiveDocument.Tables(1).Cell(12, 1).Range.InsertAfter "12"
myword.ActiveDocument.Tables(1).Cell(13, 1).Range.InsertAfter "13"
myword.ActiveDocument.Tables(1).Cell(14, 1).Range.InsertAfter "14"
myword.ActiveDocument.Tables(1).Cell(15, 1).Range.InsertAfter "15"
myword.ActiveDocument.Tables(1).Cell(16, 1).Range.InsertAfter "16"

myword.ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs.Alignment = wdAlignParagraphCenter '定位表格居中

'*****表格字号
myword.ActiveDocument.Tables(1).Cell(1, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(2, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(3, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(4, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(5, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(6, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(7, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(8, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(9, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(10, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(11, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(12, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(13, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(14, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(15, 1).Range.Font.Size = 8
myword.ActiveDocument.Tables(1).Cell(16, 1).Range.Font.Size = 8

myword.ActiveDocument.Tables(1).Cell(12, 1).Range.Font.Color = vbRed '***颜色
myword.ActiveDocument.Tables(1).Cell(13, 1).Range.Font.Color = vbRed

myword.ActiveDocument.Tables(1).Cell(12, 1).Range.Font.Bold = True
myword.ActiveDocument.Tables(1).Cell(13, 1).Range.Font.Bold = True



myword.Selection.EndKey unit:=wdStory
myword.Selection.TypeParagraph

myr.Paragraphs.Alignment = wdAlignParagraphCenter
myr.InsertAfter "aaaa" & Chr(13)
myr.Font.Name = "宋体"
myr.Font.Size = 9
myr.Font.Underline = False
myr.Font.Bold = False
myr.Start = myr.End
3 回复
#2
dushuzai2017-11-25 11:49
抱歉。中间少了一段代码 ,光标定位上行。
On Error Resume Next

Set myword = CreateObject("Word.Application")
Set myd = myword.Documents.Add
Set myr = myd.Range(Start:=0, End:=0)
myr.Paragraphs.Alignment = wdAlignParagraphCenter
myr.InsertAfter "aaaa" & Chr(13)
myr.Font.Name = "宋体"
myr.Font.Size = 9
myr.Font.Underline = False
myr.Font.Bold = False
myr.Start = myr.End
#3
你的爱8882017-11-25 13:26
62d24728c369759fc91d0f5fc6c8d3e6
#4
xyxcc1772017-11-25 13:34
错误在 myr = myd.Range(Start:=0, End:=0),这句把光标定在myd的开最前面;你声明的对象都没有用,代码太繁
Dim myword As Word.Application
 Dim myd As Word.Document
 Dim myr As Word.Range
 Dim myr1 As Word.Range
 Dim mys As Word.Selection
 Dim myt As Word.Table

myword.Selection.EndKey unit:=wdStory '***************表格光标定位
set myt= myword.ActiveDocument.Tables.Add(myword.Application.Selection.Range, 16, 1, 1, 0) '***************建立表格

with myt
.Cell(1, 1).Range.InsertAfter "1"  '访问表格
.Cell(2, 1).Range.InsertAfter "2"
.Cell(3, 1).Range.InsertAfter "3"
......
end with


myword.ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs.Alignment = wdAlignParagraphCenter '定位表格居中

'*****表格字号
......
myword.Selection.EndKey unit:=wdStory
myword.Selection.TypeParagraph
Set myr = myd.Range
myr.Paragraphs.Alignment = wdAlignParagraphCenter
myr.InsertAfter "aaaa" & Chr(13)
myr.Font.Name = "宋体"
myr.Font.Size = 9
myr.Font.Underline = False
myr.Font.Bold = False
myr.Start = myr.End
1