各位大侠好,我在用vb操作word时遇到很多问题,请大侠们不吝赐教。
我想实现如下功能:vb调用office中的word,新建word文档,并把记录输入到新建的word文档中。要输入的内容包括文字,表格和图片。
遇到的问题:各部分内容的插入点出问题。比如说我想输入一段文字然后换行输入表格,并在表格中输入数据,再换行建另一个表格并输入数据,之后再换行插入一个图片。我用下面代码去实现,结果却是后面的表格插入到了第一个表格的第一个单元格中,图片又插入到了第二个表格的第一个单元格中。请大侠们指点!谢谢!Private Sub Command2_Click()
'把记录输出到word文档中的表格内
Dim mywrd As Word.Application
Dim mybook As Word.Document
Dim mytable As Word.Table
Dim a As String
Dim b As String
a = "myname"
b = "10吨"
Set mywrd = CreateObject("Word.Application")
Set mybook = mywrd.Documents.Add
mywrd.Visible = True
mybook.Activate
mywrd.Options.CheckGrammarAsYouType = False
mywrd.Options.CheckSpellingAsYouType = False
mywrd.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
mywrd.Selection.Font.Color = wdColorBlack
mywrd.Selection.Font.Name = "宋体"
mywrd.Selection.Font.Size = 12
mywrd.Selection.TypeText ("记录输出到表格中")
mywrd.Selection.TypeParagraph
mywrd.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
mywrd.Selection.Font.Color = wdColorBlue
mywrd.Selection.TypeText ("插入一个表格,然后在表格中放入要输出的数据。")
mywrd.Selection.Font.Color = wdColorBlack
mywrd.Selection.TypeParagraph '分段操作
mywrd.Selection.Font.Size = 10.5
mywrd.Selection.TypeText ("基本参数表:")
mywrd.Selection.TypeParagraph
'插入表格
Set mytable = mywrd.ActiveDocument.Tables.Add(mywrd.Selection.Range, 5, 2)
mywrd.Selection.TypeParagraph
With Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
' 插入表格并把记录填入
mytable.Cell(1, 1).Range.InsertAfter ("参数名称")
mytable.Cell(1, 2).Range.InsertAfter ("参数值")
mytable.Cell(2, 1).Range.InsertAfter ("跨度")
mytable.Cell(2, 2).Range.InsertAfter (a)
mytable.Cell(3, 1).Range.InsertAfter "吊重"
mytable.Cell(3, 2).Range.InsertAfter b
mywrd.Selection.TypeParagraph
mywrd.Selection.TypeText ("主主梁参数:")
'插入表格
Set mytable = mywrd.ActiveDocument.Tables.Add(mywrd.Selection.Range, 5, 2)
mywrd.Selection.TypeParagraph
With Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
' 插入表格并把记录填入
mytable.Cell(1, 1).Range.InsertAfter ("参数名称")
mytable.Cell(1, 2).Range.InsertAfter ("参数值")
mytable.Cell(2, 1).Range.InsertAfter ("跨度")
mytable.Cell(2, 2).Range.InsertAfter (a)
mytable.Cell(3, 1).Range.InsertAfter "吊重"
mytable.Cell(3, 2).Range.InsertAfter b
'插入图片
mywrd.Selection.InlineShapes.AddPicture FileName:=App.Path + "/1.jpg", linktofile:=False, savewithdocument:=True
mybook.SaveAs (App.Path + "/记录.doc")
mybook.Close
mywrd.Quit
Set mywrd = Nothing MsgBox "记录输出完毕", vbOKOnly, "成功输出"End Sub
我想实现如下功能:vb调用office中的word,新建word文档,并把记录输入到新建的word文档中。要输入的内容包括文字,表格和图片。
遇到的问题:各部分内容的插入点出问题。比如说我想输入一段文字然后换行输入表格,并在表格中输入数据,再换行建另一个表格并输入数据,之后再换行插入一个图片。我用下面代码去实现,结果却是后面的表格插入到了第一个表格的第一个单元格中,图片又插入到了第二个表格的第一个单元格中。请大侠们指点!谢谢!Private Sub Command2_Click()
'把记录输出到word文档中的表格内
Dim mywrd As Word.Application
Dim mybook As Word.Document
Dim mytable As Word.Table
Dim a As String
Dim b As String
a = "myname"
b = "10吨"
Set mywrd = CreateObject("Word.Application")
Set mybook = mywrd.Documents.Add
mywrd.Visible = True
mybook.Activate
mywrd.Options.CheckGrammarAsYouType = False
mywrd.Options.CheckSpellingAsYouType = False
mywrd.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
mywrd.Selection.Font.Color = wdColorBlack
mywrd.Selection.Font.Name = "宋体"
mywrd.Selection.Font.Size = 12
mywrd.Selection.TypeText ("记录输出到表格中")
mywrd.Selection.TypeParagraph
mywrd.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
mywrd.Selection.Font.Color = wdColorBlue
mywrd.Selection.TypeText ("插入一个表格,然后在表格中放入要输出的数据。")
mywrd.Selection.Font.Color = wdColorBlack
mywrd.Selection.TypeParagraph '分段操作
mywrd.Selection.Font.Size = 10.5
mywrd.Selection.TypeText ("基本参数表:")
mywrd.Selection.TypeParagraph
'插入表格
Set mytable = mywrd.ActiveDocument.Tables.Add(mywrd.Selection.Range, 5, 2)
mywrd.Selection.TypeParagraph
With Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
' 插入表格并把记录填入
mytable.Cell(1, 1).Range.InsertAfter ("参数名称")
mytable.Cell(1, 2).Range.InsertAfter ("参数值")
mytable.Cell(2, 1).Range.InsertAfter ("跨度")
mytable.Cell(2, 2).Range.InsertAfter (a)
mytable.Cell(3, 1).Range.InsertAfter "吊重"
mytable.Cell(3, 2).Range.InsertAfter b
mywrd.Selection.TypeParagraph
mywrd.Selection.TypeText ("主主梁参数:")
'插入表格
Set mytable = mywrd.ActiveDocument.Tables.Add(mywrd.Selection.Range, 5, 2)
mywrd.Selection.TypeParagraph
With Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
' 插入表格并把记录填入
mytable.Cell(1, 1).Range.InsertAfter ("参数名称")
mytable.Cell(1, 2).Range.InsertAfter ("参数值")
mytable.Cell(2, 1).Range.InsertAfter ("跨度")
mytable.Cell(2, 2).Range.InsertAfter (a)
mytable.Cell(3, 1).Range.InsertAfter "吊重"
mytable.Cell(3, 2).Range.InsertAfter b
'插入图片
mywrd.Selection.InlineShapes.AddPicture FileName:=App.Path + "/1.jpg", linktofile:=False, savewithdocument:=True
mybook.SaveAs (App.Path + "/记录.doc")
mybook.Close
mywrd.Quit
Set mywrd = Nothing MsgBox "记录输出完毕", vbOKOnly, "成功输出"End Sub
解决方案 »
- 二台MSSQL服务器,一台能连接成功,另一台一址提示如下错误。
- 用 timegettime可以获取毫秒的时间,请问如何获取微妙的时间?谢谢
- 在sql中如何插入identity列数据??请大家帮忙
- 再见了 大家 不想做程序了,现在开始做商务代表了。。。
- 请教VB6中的Msflexgrid控件Compare事件的用法
- 急等您的答案:用左联接生成的DBgrid不能进行删除和修改吗?怎么办呀?
- 关于“网上激活”
- 第一问:用MSFlexGrid显示数据怎么设置他显示的字段个数?第二问:我怎样打印显示出来的表呢?表中的一行能否双击弹出?
- 有一点点难度,但是在这里应该会很快解决,那就给50分吧!求技巧。
- 十个数组合有多少种算法
- 关于文本文件仲的字符替换,我已经试过狠多方法,亦看了此网站很多的已提问问题和答案,但都找不出解决方法,本人初学VB,所以请各位帮帮忙!
- vb6 ,msinet.ocx , POST 方式提交url ,现提示 35760 canot coerce type ,怎么解决,谢谢!
wddoc.Books("page1a").DeleteSet wdtab = wddoc.Tables.Add(wddoc.Books("table").Range, 4, 10, defaultTableBehavior:=1, AutoFitBehavior:=0)
wddoc.Books("table").Delete