各位大侠好,我在用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

解决方案 »

  1.   

    不如事先做个模板放资源文件中,模板中多放些标签,使用时,释放到文件,在不同的标签处插入你的文字、表格、图片,完成后删除标签,保存文件wddoc.Books("page1a").Range.text = text1
    wddoc.Books("page1a").DeleteSet wdtab = wddoc.Tables.Add(wddoc.Books("table").Range, 4, 10, defaultTableBehavior:=1, AutoFitBehavior:=0)
    wddoc.Books("table").Delete