Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Visual-Studio VB.NET
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
Posted 30-Sep-12 12:53pm
icecode346
Edited 1-Oct-12 16:15pm
v3
Comments
Kuthuparakkal at 30-Sep-12 23:51pm
   
What's the issue ?
Sergey Alexandrovich Kryukov at 1-Oct-12 0:50am
   
"Not working" is not informative. This is not a question.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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()
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 268
3 Sergey Alexandrovich Kryukov 263
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100