在word录制宏,看代码就知道了

解决方案 »

  1.   

    Private Sub Command2_Click()Dim appid, returnvalue
    Screen.MousePointer = 11 '新设屏幕鼠标指针
    Set msword = CreateObject("word.basic") '创建对象msword
    appid = Shell("C:\Program Files\Microsoft Office\Office\winword.exe", 0)
    msword.AppActivate "microsoft word" '启动microsoft word
    datatrans '调用数据传递过程
    Screen.MousePointer = 0 '恢复屏幕鼠标设置
    msword.fileprint
    End Sub
    Sub datatrans()
    Dim i As Integer, j As Integer, col As Integer, row As Integer
    Dim cellcontent As String
    Me.Hide '隐藏vb窗体
    Cols = 7 '表格的列数
    rows = 50 '........行数
    '创建word 文件与其中的报表
    msword.filenewdefault
    msword.MsgBox "wait a moment", "", -1msword.screenupdating 1
    msword.tableinserttable , Cols, rows, , , 16, 167
    msword.startofdocument
    For k = 1 To Cols
        msword.Insert cellcontent$
        msword.nextcell
    Next k
    '将datagrid中的数据传递到word 报表中
    Adodc1.Recordset.MoveFirst
    jj = 0
    Do While Adodc1.Recordset.EOF() = False
       jj = jj + 1
       For i = 0 To Cols - 1
              DataGrid1.col = i
                If IsNull(DataGrid1.Text) Then
                   cellcontent$ = ""
                   Else
                    cellcontent$ = DataGrid1.Text
                 End If
              msword.Insert cellcontent$
              msword.nextcell
       Next i
       If jj = 40 Then '用于控制传递数据的总行数
          Exit Do
          
       End If
       Adodc1.Recordset.MoveNext
    Loop
    msword.Tabledeleterow
    msword.startofdocument
    msword.tableselectrow
    msword.tableheadings 1
    msword.centerpara
    msword.screenrefresh
    msword.screenupdating 1
    msword.MsgBox "end", "", -1
    Me.Show '显示被隐藏的vb窗体
    End Sub
    Private Sub Form_Load()
    Dim rs As Recordset
    Adodc1.RecordSource = "select room_no ,room_large,room_name ,room_mode,room_time1,room_time2,room_time3 from _test_room"
    Adodc1.Refresh
    Set rs = Adodc1.Recordset
    'DataGrid1.DataSource = Adodc1
    End Sub
      

  2.   

    上面的源代码是从数据库查出来 传到word 中的
    如没有数据库
    可以把数据库查询的地方不要
      

  3.   

     to liuxing23(流星):你好,有没有 visual basic for application 的例子
                        word basic 好像老了一点
                        感谢您的帮助
     to ciml(镜子) :你的方法好像不错,让我好好
                     研究一下,谢谢您
     to binghuodao(冰火岛) :好像是 Vbabdr8.hlp .谢谢您