Word document from a DataGrid?

  • Thread starter Thread starter Webbyz
  • Start date Start date
W

Webbyz

Another problem that I am having is that I want to make some type of
word or text document that is populated with data from my datagrid. Is
this possable? And how do I keep ot formatted? I.E. I have 7 columns
with values in all of them. How do I keep the columns straight on the
printout?

Thanks!
 
To use Word, add a refernce to Microsoft Word 9.0( or 10.0) Object Library.
Then add a Class module and add code similar to:

Public Class WordClass
Public WithEvents WordApp As Word.Application


Public Sub New()
WordApp = New Word.Application
End Sub
End Class

To get data from a datagrid into a Word document, use the original dataset
that filled the datagrid, create a Word table and populate it with code
similar to:

Dim Word As New WordClass
Dim RowCounter As Int16, ColumnCounter as Int16

' Add a blank document
Word.WordApp.Documents.Add()

' Add a table for the data to go into
Word.WordApp.Selection.Tables.Add(Range:=Word.WordApp.Selection.Range,
numrows:=1, numcolumns:=6)

' Put the data from the dataset into the table
For RowCounter = 0 To ds.Tables("Table").Rows.Count - 1
For ColumnCounter = 0 to ds.Tables("Table").Columns.Count - 1
Word.WordApp.Selection.TypeText(ds.Tables("Table").Rows(RowCounter).Item(ColumnCounter))
Word.WordApp.Selection.MoveRight(Unit:=12)
Next ColumnCounter
Next RowCounter

' Show the document
Word.WordApp.Visible = True

HTH
Helen
 
Helen,

Nice answer, however have you any reason to slow your program down by
letting it every time convert an integer (the 32bit register size on 32bit
computers) to an int16?

Net is at the moment only working at 32bit and 64bits computers.

As addition, as far as I have understand in now, do you need in 64bits
computers have to change in future all your programs by hand to make it
optimized by changing 'integer' to 'long' if you want that to work the most
optimized. Than that is only optimized for 64 bits computers and do you have
the same effect as now in your programs with that int16 on 32bit computers

It is not delaying that much that the last is worth the work direct.

Just my thought,

Cor
 
Seems like it would work but I do not populate via a dataset. Here is
how I populate....

Dim cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=wasa.mdb;User Id=admin;Password=;"
Dim conn As OleDb.OleDbConnection = New
OleDb.OleDbConnection(cs)
Dim dt As New DataTable
Dim sql As String = "SELECT g.game_id, g.game_date,
g.game_time, l.desc, gs.desc, ps.desc, referee.first_name,
referee.last_name FROM ((((game AS g LEFT JOIN ref_game AS rg ON
g.game_id = rg.game_id) LEFT JOIN referee ON rg.ref_id =
referee.ref_id) LEFT JOIN game_status AS gs ON g.played = gs.id) LEFT
JOIN payment_status AS ps ON rg.paid = ps.id) LEFT JOIN league AS l ON
g.game_type = l.league_id ORDER BY referee.last_name;"
Dim da As New OleDbDataAdapter(sql, conn)
da.Fill(dt)


Dim i As Integer = 0
For Each dr As DataRow In dt.Rows
lvGames.Items.Add(dr.Item(0).ToString)
lvGames.Items(i).SubItems.Add(dr.Item(1).ToString)
lvGames.Items(i).SubItems.Add(dr.Item(2).ToString)
lvGames.Items(i).SubItems.Add(dr.Item(3).ToString)
lvGames.Items(i).SubItems.Add(dr.Item(4).ToString)
lvGames.Items(i).SubItems.Add(dr.Item(6).ToString & " " &
dr.Item(7).ToString)
lvGames.Items(i).SubItems.Add(dr.Item(5).ToString)
i += 1


So there is no DS to pull from. Any other way to do this?
 
Webbyz,

This one I have searched for it is often provided by Paul Clement, the time
in the USA is now become daytime so maybe he will give a reaction soon.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top