This project has moved. For the latest updates, please go here.
' insert a table to document in Word
' taken from Example02.vb
'
Private Sub Example02Main()

   ' start word and turn off msg boxes
   Dim wordApplication As New Word.Application
   wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone

   ' add a new document
   Dim newDocument As Word.Document
   newDocument = wordApplication.Documents.Add()

   ' add a table
   Dim table As Word.Table
   table = newDocument.Tables.Add(wordApplication.Selection.Range, 3, 2)

   'insert some text into the cells
   table.Cell(1, 1).Select()
   wordApplication.Selection.TypeText("This")

   table.Cell(1, 2).Select()
   wordApplication.Selection.TypeText("table")

   table.Cell(2, 1).Select()
   wordApplication.Selection.TypeText("was")

   table.Cell(2, 2).Select()
   wordApplication.Selection.TypeText("created")

   table.Cell(3, 1).Select()
   wordApplication.Selection.TypeText("by")

   table.Cell(3, 2).Select()
   wordApplication.Selection.TypeText("NetOffice")

   'save the document
   Dim fileExtension As String = GetDefaultExtension(wordApplication)
   Dim documentFile As String = _
String.Format("{0}\Example02{1}", Application.StartupPath, fileExtension) newDocument.SaveAs(documentFile) ' close word and dispose reference wordApplication.Quit() wordApplication.Dispose() Dim fDialog As New FinishDialog("Document saved.", documentFile) fDialog.ShowDialog(Me) End Sub #Region "Helper" ''' <summary> ''' returns the valid file extension for the instance. for example ".doc" or ".docx" ''' </summary> ''' <param name="application">the instance</param> ''' <returns>the extension</returns> ''' <remarks></remarks> Private Function GetDefaultExtension(ByVal application As Word.Application) As String Dim version As Double = _ Convert.ToDouble(application.Version, CultureInfo.InvariantCulture)
If (version >= 12.0) Then Return ".docx" Else Return ".doc" End If End Function #End Region

Last edited Jun 13, 2012 at 8:25 AM by SebastianDotNet, version 6

Comments

No comments yet.