EDN Admin
Well-known member
Hi all,
This error is ridiculously common, and Im not too sure why it would be caused this time.
What Im trying to do is export two datagridviews to word, using code from the following website: http://www.vbforums.com/showthread.php?t=626953 http://www.vbforums.com/showthread.php?t=626953 . Its worked previously
perfectly, and does for the other grids, but at the moment I have the object reference error for the MonthlyCashflow and LoanAnalysis grids. The rows and columns in the grids come from calculations and not my database.
<span style="font-size:11px; white-spacere And heres my code:
<pre style="font-size:11px; padding:0px; margin:0px <span style="color:blue Private <span style="color:blue Sub btnPreview_Click(<span style="color:blue ByVal sender <span style="color:blue As System.Object, <span style="color:blue ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue Handles btnPreview.Click
<span style="color:blue Dim conn <span style="color:blue As <span style="color:blue New SqlConnection
conn = GetConnect()
Cursor.Current = Cursors.WaitCursor
<span style="color:blue Dim oWord <span style="color:blue As Word.Application = <span style="color:blue DirectCast(CreateObject(<span style="color:#a31515 "Word.Application"), Word.Application)
<span style="color:blue Dim oDoc <span style="color:blue As Word.Document = oWord.Documents.Open(IO.Path.Combine(<span style="color:blue My.Application.Info.DirectoryPath, <span style="color:#a31515 "Q:\StaffNatasha BynonPersonalScenarios.docx"))
oWord.Visible = <span style="color:blue True
dgvprint1(oWord, oDoc)
dgvprint2(oWord, oDoc)
Cursor.Current = Cursors.<span style="color:blue Default
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Private <span style="color:blue Sub dgvprint1(<span style="color:blue ByVal oWord <span style="color:blue As Word.Application, <span style="color:blue ByVal oDoc <span style="color:blue As Word.Document)
<span style="color:green Bookmarks are "grid1" (relates to DataGridView1), + "grid2" (relates to DataGridView2)
<span style="color:blue Dim dgvs(,) <span style="color:blue As <span style="color:blue Object = {{dgvMonthlyCashflow, <span style="color:#a31515 "MonthlyCashflow"}, {dgvLoanAnalysis, <span style="color:#a31515 "LoanAnalysis"}}
<span style="color:blue For x <span style="color:blue As <span style="color:blue Integer = 0 <span style="color:blue To dgvs.GetUpperBound(0)
<span style="color:blue Dim headers = (<span style="color:blue From ch <span style="color:blue In <span style="color:blue DirectCast(dgvs(x, 0), DataGridView).Columns _
<span style="color:blue Let header = <span style="color:blue DirectCast(<span style="color:blue DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
<span style="color:blue Select header.Value).ToArray()
<span style="color:blue Dim headerText() <span style="color:blue As <span style="color:blue String = Array.ConvertAll(headers, <span style="color:blue Function(v) v.ToString)
<span style="color:blue Dim items() = (<span style="color:blue From r <span style="color:blue In <span style="color:blue DirectCast(dgvs(x, 0), DataGridView).Rows _
<span style="color:blue Let row = <span style="color:blue DirectCast(r, DataGridViewRow) _
<span style="color:blue Where <span style="color:blue Not row.IsNewRow _
<span style="color:blue Select (<span style="color:blue From cell <span style="color:blue In row.Cells _
<span style="color:blue Let c = <span style="color:blue DirectCast(cell, DataGridViewCell) _
<span style="color:blue Select c.Value).ToArray()).ToArray()
<span style="color:blue Dim table <span style="color:blue As <span style="color:blue String = <span style="color:blue String.<span style="color:blue Join(vbTab, headerText) & Environment.NewLine
<span style="color:blue For <span style="color:blue Each a <span style="color:blue In items
<span style="color:blue Dim t() <span style="color:blue As <span style="color:blue String = Array.ConvertAll(a, <span style="color:blue Function(v) v.ToString) <span style="color:#008000; font-size:12px Error here! <br/>
table &= <span style="color:blue String.<span style="color:blue Join(vbTab, t) & Environment.NewLine
<span style="color:blue Next
table = table.TrimEnd(<span style="color:blue CChar(Environment.NewLine))
Clipboard.SetText(table)
<span style="color:blue Dim oTable <span style="color:blue As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item(dgvs(x, 1).ToString).Range, items.<span style="color:blue Count + 1, headers.<span style="color:blue Count)
oTable.Range.Paste()
<span style="color:blue Next
<span style="color:blue End <span style="color:blue Sub[/code]
The same error appears for the other sub, dgvprint2, which contains two more tables roughly the same size.
What could be causing this? I think its that items() isnt returning anything, but it should be because theres a good 10 columns or so in each datagrid, with up 60 rows in one, and 15 in the other.
Im at a loss on how to fix this, any suggestions?
Thank you in advance,
Verraine
<br/>
<br/>
<br/>
<br/>
View the full article
This error is ridiculously common, and Im not too sure why it would be caused this time.
What Im trying to do is export two datagridviews to word, using code from the following website: http://www.vbforums.com/showthread.php?t=626953 http://www.vbforums.com/showthread.php?t=626953 . Its worked previously
perfectly, and does for the other grids, but at the moment I have the object reference error for the MonthlyCashflow and LoanAnalysis grids. The rows and columns in the grids come from calculations and not my database.
<span style="font-size:11px; white-spacere And heres my code:
<pre style="font-size:11px; padding:0px; margin:0px <span style="color:blue Private <span style="color:blue Sub btnPreview_Click(<span style="color:blue ByVal sender <span style="color:blue As System.Object, <span style="color:blue ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue Handles btnPreview.Click
<span style="color:blue Dim conn <span style="color:blue As <span style="color:blue New SqlConnection
conn = GetConnect()
Cursor.Current = Cursors.WaitCursor
<span style="color:blue Dim oWord <span style="color:blue As Word.Application = <span style="color:blue DirectCast(CreateObject(<span style="color:#a31515 "Word.Application"), Word.Application)
<span style="color:blue Dim oDoc <span style="color:blue As Word.Document = oWord.Documents.Open(IO.Path.Combine(<span style="color:blue My.Application.Info.DirectoryPath, <span style="color:#a31515 "Q:\StaffNatasha BynonPersonalScenarios.docx"))
oWord.Visible = <span style="color:blue True
dgvprint1(oWord, oDoc)
dgvprint2(oWord, oDoc)
Cursor.Current = Cursors.<span style="color:blue Default
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Private <span style="color:blue Sub dgvprint1(<span style="color:blue ByVal oWord <span style="color:blue As Word.Application, <span style="color:blue ByVal oDoc <span style="color:blue As Word.Document)
<span style="color:green Bookmarks are "grid1" (relates to DataGridView1), + "grid2" (relates to DataGridView2)
<span style="color:blue Dim dgvs(,) <span style="color:blue As <span style="color:blue Object = {{dgvMonthlyCashflow, <span style="color:#a31515 "MonthlyCashflow"}, {dgvLoanAnalysis, <span style="color:#a31515 "LoanAnalysis"}}
<span style="color:blue For x <span style="color:blue As <span style="color:blue Integer = 0 <span style="color:blue To dgvs.GetUpperBound(0)
<span style="color:blue Dim headers = (<span style="color:blue From ch <span style="color:blue In <span style="color:blue DirectCast(dgvs(x, 0), DataGridView).Columns _
<span style="color:blue Let header = <span style="color:blue DirectCast(<span style="color:blue DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
<span style="color:blue Select header.Value).ToArray()
<span style="color:blue Dim headerText() <span style="color:blue As <span style="color:blue String = Array.ConvertAll(headers, <span style="color:blue Function(v) v.ToString)
<span style="color:blue Dim items() = (<span style="color:blue From r <span style="color:blue In <span style="color:blue DirectCast(dgvs(x, 0), DataGridView).Rows _
<span style="color:blue Let row = <span style="color:blue DirectCast(r, DataGridViewRow) _
<span style="color:blue Where <span style="color:blue Not row.IsNewRow _
<span style="color:blue Select (<span style="color:blue From cell <span style="color:blue In row.Cells _
<span style="color:blue Let c = <span style="color:blue DirectCast(cell, DataGridViewCell) _
<span style="color:blue Select c.Value).ToArray()).ToArray()
<span style="color:blue Dim table <span style="color:blue As <span style="color:blue String = <span style="color:blue String.<span style="color:blue Join(vbTab, headerText) & Environment.NewLine
<span style="color:blue For <span style="color:blue Each a <span style="color:blue In items
<span style="color:blue Dim t() <span style="color:blue As <span style="color:blue String = Array.ConvertAll(a, <span style="color:blue Function(v) v.ToString) <span style="color:#008000; font-size:12px Error here! <br/>
table &= <span style="color:blue String.<span style="color:blue Join(vbTab, t) & Environment.NewLine
<span style="color:blue Next
table = table.TrimEnd(<span style="color:blue CChar(Environment.NewLine))
Clipboard.SetText(table)
<span style="color:blue Dim oTable <span style="color:blue As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item(dgvs(x, 1).ToString).Range, items.<span style="color:blue Count + 1, headers.<span style="color:blue Count)
oTable.Range.Paste()
<span style="color:blue Next
<span style="color:blue End <span style="color:blue Sub[/code]
The same error appears for the other sub, dgvprint2, which contains two more tables roughly the same size.
What could be causing this? I think its that items() isnt returning anything, but it should be because theres a good 10 columns or so in each datagrid, with up 60 rows in one, and 15 in the other.
Im at a loss on how to fix this, any suggestions?
Thank you in advance,
Verraine
<br/>
<br/>
<br/>
<br/>
View the full article