HI,

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..^^

Recommended Answers

All 3 Replies

Hi,
If you look at Interop it should help. Here is a tutorial

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
        Try
            '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.
            oDoc.Range.Delete()

            oPara1 = oDoc.Content.Paragraphs.Add
            oPara1.Range.MoveStart()
            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

            oPara1.Range.InsertParagraphAfter()

            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)
                    Next

                Else

                    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))

                    Next
                End If

            Next

            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

            'tlb.Columns.AutoFit()
            tlb.AutoFitBehavior(wdAutoFitBehavior1)
            oDoc.ShowGrammaticalErrors = False
            oDoc.ShowSpellingErrors = False

            oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CType(CInt(Word.WdSeekView.wdSeekCurrentPageHeader), WdSeekView)
            oDoc.Content.Application.Selection.TypeText(Text:=dbName)
            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)
            oDoc.Content.Application.Selection.TypeText(Text:=Now.ToShortDateString)
            '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


        'oDoc.Close()
        'oApp.Quit()

    End Sub


End Class

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.