I am saving my form to word document. But the result, looks too simple. How do i insert table or insert image or maybe,could change the font style and so on??.. if you can give links or tutorials..anything that could help me..i would appreciate it so much. Thank you in advance..^^

3 Years
Discussion Span
Last Post by missy_mi
Featured Replies
  • Hi, If you look at Interop it should help. Here is a [tutorial](https://support.microsoft.com/kb/316383) Read More


The following code shows how to put a table and some other minor things into a word doc. It's part of a database schema app. displaying the table name column names and their datatype ordinal position and if nullable. So the dbname and the table name are not important just strings. the datatable has to have data with column names. You will also need a reference to Microsoft.Office.Interop.Word

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word

Public Class WordDoc

    ''' <summary>
    ''' dTbl - The table you wish to display
    ''' tblName - Name of the Table 
    ''' dbName - the database File Path and Name
    ''' </summary>
    ''' <param name="dTbl"></param>
    ''' <param name="tblName"></param>
    ''' <param name="dbName"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal dTbl As DataTable, ByVal tblName As String, ByVal dbName As String)

        Dim oApp As Word.Application
        Dim oDoc As Word.Document
        Dim oPara1 As Word.Paragraph
            'Start a new document in Word                    
            oApp = CType(CreateObject("Word.Application"), Word.Application)

            oDoc = oApp.Documents.Add()

            Dim rws As Integer = dTbl.Rows.Count
            Dim cols As Integer = dTbl.Columns.Count

            ' Clear out any existing information.

            oPara1 = oDoc.Content.Paragraphs.Add
            oPara1.Range.Text = tblName & " TABLE"
            oPara1.Range.Font.Size = 10
            oPara1.Range.Font.Bold = CInt(True)
            oPara1.Format.SpaceAfter = 4
            oPara1.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft


            Dim tlb As Word.Table
            tlb = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, CInt(rws + 1), CInt(cols))

            oApp.Visible = True
            tlb.Style = "Table Grid 8" '"wdTableFormatGrid8"  '"Table Grid 7" "Table Columns 5"

                'Data Rows 
            For j As Integer = 0 To CInt(rws) 'no -1 here as the headers need to be added

                If j = 0 Then
                    For i As Integer = 0 To CInt(cols - 1)
                        tlb.Cell(j + 1, i + 1).Range.Text = CStr(dTbl.Columns(i).ColumnName)


                    For i As Integer = 0 To CInt(cols - 1)
                        Dim temp As String = CStr(dTbl.Rows(j - 1).Item(i))
                        tlb.Cell(j + 1, i + 1).Range.Text = CStr(dTbl.Rows(j - 1).Item(i))

                End If


            Dim wdAutoFitBehavior1 As WdAutoFitBehavior = WdAutoFitBehavior.wdAutoFitWindow

            'Selection.Style = ActiveDocument.Styles("Heading 1")
            'Selection.Style = ActiveDocument.Styles("Heading 2")
            'Selection.Style = ActiveDocument.Styles("Quote")

            tlb.AllowPageBreaks = False

            oDoc.ShowGrammaticalErrors = False
            oDoc.ShowSpellingErrors = False

            oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CType(CInt(Word.WdSeekView.wdSeekCurrentPageHeader), WdSeekView)
            oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CType(CInt(Word.WdSeekView.wdSeekMainDocument), WdSeekView)

            oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CType(CInt(Word.WdSeekView.wdSeekCurrentPageFooter), WdSeekView)
            'return to the main document        
            oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CType(CInt(Word.WdSeekView.wdSeekMainDocument), WdSeekView)

            Dim dir As String = My.Computer.FileSystem.SpecialDirectories.Desktop

            oDoc.SaveAs(dir & "\" & tblName & "_Schema.docx")

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "WORD ERROR")
        End Try


    End Sub

End Class

Thank you..i solved it..thanks for the link @G _WADDLE..that link helps a lot..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.