具有单词自动化功能的数据库应用程序 [英] Database application with word automation

查看:60
本文介绍了具有单词自动化功能的数据库应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个应用程序,但似乎无法将datagridview中的数据导出到Microsoft Word文档中,这是我当前的代码,并且无法继承到Word文档中.我在"With Me.WaterQCDataGridView.Rows(indRow)"上出现异常错误

I have an application that i am working on and I seem not to be able to have the data from the datagridview export to a Microsoft Word Document here is my current code and it is not carrying over to a word document. I get an exception error on "With Me.WaterQCDataGridView.Rows(indRow)"

Public Class Water_QC 
   
    Private Sub WaterQCBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WaterQCBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.WaterQCBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.WaterQCDataSet)
    End Sub
    Private Sub Water_QC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'WaterQCDataSet.WaterQC' table. You can move, or remove it, as needed.
        Me.WaterQCTableAdapter.Fill(Me.WaterQCDataSet.WaterQC)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim indRow As Integer = 1
        For i As Integer = 0 To 10
            indRow = Me.WaterQCDataGridView.RowCount
            Try
                With Me.WaterQCDataGridView.Rows(indRow)
                    .Cells("Tank Number").Value =
                    .Cells("Clarity, color, and odor").Value =
                    .Cells("Sample Temperature").Value = i
                End With
            Catch ex As FormatException
                i = False
            End Try
        Next
                
        Dim objWordApp As Object
        Dim objWordDoc As Object
        objWordApp = CreateObject("Word.Application")
        objWordDoc = objWordApp.documents.add()
        objWordDoc.tables.add(objWordDoc.range(0, 0), 11, 3)
        With objWordDoc.tables(1)
            .borders.InsideLineStyle = 1
            .borders.OutsideLineStyle = 1
            For i As Integer = 0 To 10
                .rows(i + 1).cells(1).range.text = Me.WaterQCDataGridView.Rows(i).Cells("Tank Number").Value
                .rows(i + 1).cells(2).range.text = Me.WaterQCDataGridView.Rows(i).Cells("Clarity,color,and odor").Value
                .rows(i + 1).cells(3).range.text = Me.WaterQCDataGridView.Rows(i).Cells("Sample Temperature").Value
            Next
        End With
        objWordApp.visible = True
        objWordApp = Nothing
    End Sub
End Class

推荐答案

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Create Word Application
        Dim oWord As Word.Application = CreateObject("Word.Application")
        ' Create new word document
        Dim oDoc As Word.Document = oWord.Documents.Add()
        oWord.Visible = True

        'Insert a 3 x 5 table and fill it with specific data
        Dim r As Integer, c As Integer
        Dim oTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
        oTable.Range.ParagraphFormat.SpaceAfter = 6
        For r = 1 To 3
            For c = 1 To 5
'IN THIS PLACE ADD YOUR TEXTBOX DATA
                oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c
            Next
        Next
        'make the first row bold and italic
        oTable.Rows.Item(1).Range.Font.Bold = True
        oTable.Rows.Item(1).Range.Font.Italic = True
        ' Save this word document
        oDoc.SaveAs("C:\myfile.doc", True)
        oDoc.Close()
        oWord.Application.Quit()


这篇关于具有单词自动化功能的数据库应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆