Results 1 to 4 of 4

Thread: [FAQ's: OD] How do I add/modify/read a table programmatically?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    [FAQ's: OD] How do I add/modify/read a table programmatically?

    To programmatically add, modify, or read a table cell in a Word document:




    Word 2003 And VB 6 Code Example (Add Table):

    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Word xx.0 Object Library
    3. Public Function AddTable(ByVal iRows As Integer, ByVal iCols As Integer) As Boolean
    4.    
    5.     Dim oApp As Word.Application
    6.     Dim oDoc As Word.Document
    7.     Dim oTable As Word.Table
    8.     Dim oRowHeader As Word.Row
    9.     Dim x As Integer
    10.    
    11.     Set oApp = New Word.Application
    12.     'Open either a blank new document or...
    13.     Set oDoc = oApp.Documents.Add
    14.     'open an exisiting document
    15.     'Set oDoc = oApp.Documents.Open("C:\Document1.doc")
    16.     oDoc.Activate
    17.     'Move to the end of the document
    18.     oApp.Selection.Move Unit:=wdStory
    19.     oApp.Selection.TypeParagraph
    20.     oApp.Selection.TypeParagraph
    21.     'Add a new table
    22.     Set oTable = oDoc.Tables.Add(Range:=Selection.Range, NumRows:=iRows, NumColumns:=iCols, _
    23.     DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow)
    24.     'Format the table and populate
    25.     With oTable
    26.         If .Style <> "Table Grid" Then
    27.             .Style = "Table Grid"
    28.         End If
    29.         .ApplyStyleHeadingRows = True
    30.         .ApplyStyleLastRow = True
    31.         .ApplyStyleFirstColumn = True
    32.         .ApplyStyleLastColumn = True
    33.         'Populate and format the table header
    34.         Set oRowHeader = oTable.Rows.Item(1)
    35.         oRowHeader.Shading.Texture = wdTextureNone
    36.         oRowHeader.Shading.ForegroundPatternColor = wdColorAutomatic
    37.         oRowHeader.Shading.BackgroundPatternColor = wdColorGray25
    38.         For x = 1 To iCols
    39.             'Bold on for the cell
    40.             .Cell(1, x).Range.Font.Bold = True
    41.             .Cell(1, x).Range.Text = "ColumnHeader" & x
    42.         Next
    43.     End With
    44.     'Clean up and leave document open
    45.     Set oRowHeader = Nothing
    46.     Set oTable = Nothing
    47.     Set oDoc = Nothing
    48.     Set oApp = Nothing
    49.    
    50. End Function
    Last edited by RobDog888; Aug 23rd, 2006 at 04:20 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I add/modify/read a table programmatically?



    Word 2003 And VB 6 Code Example (Read Table):
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Word xx.0 Object Library
    3. 'Add a MS Flexgrid control and a Command Button.
    4. Private Sub Command4_Click()
    5.  
    6.     Dim oApp As Word.Application
    7.     Dim oDoc As Word.Document
    8.     Dim oTable As Word.Table
    9.     Dim x As Integer
    10.     Dim y As Integer
    11.    
    12.     Set oApp = New Word.Application
    13.     'open an exisiting document
    14.     Set oDoc = oApp.Documents.Open("C:\Document1.doc")
    15.     Set oTable = oDoc.Tables.Item(1)
    16.     With oTable
    17.         MSFlexGrid1.AllowUserResizing = flexResizeColumns
    18.         MSFlexGrid1.Cols = .Columns.Count
    19.         MSFlexGrid1.Rows = .Rows.Count
    20.         MSFlexGrid1.Clear
    21.         'Set up header row
    22.         For y = 0 To .Columns.Count - 1
    23.             MSFlexGrid1.Row = 0
    24.             MSFlexGrid1.Col = y
    25.             'Trim off the carriage return (chr 13 and 7)
    26.             MSFlexGrid1.Text = Replace(.Cell(1, y + 1).Range.Text, Chr(13) & Chr(7), vbNullString)
    27.         Next
    28.         'Read the cells data into the ms flexgrid control
    29.         For x = 1 To .Rows.Count - 1
    30.             For y = 0 To .Columns.Count - 1
    31.                 MSFlexGrid1.Col = y
    32.                 MSFlexGrid1.Row = x
    33.                 MSFlexGrid1.Text = Replace(.Cell(x + 1, y + 1).Range.Text, Chr(13) & Chr(7), vbNullString)
    34.             Next
    35.         Next
    36.     End With
    37.     'Clean up and close document
    38.     Set oTable = Nothing
    39.     oDoc.Close SAveChanges:=False
    40.     Set oDoc = Nothing
    41.     oApp.Quit
    42.     Set oApp = Nothing
    43.  
    44. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I add/modify/read a table programmatically?



    Word 2003 And VB 6 Code Example (Read Table):
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Word xx.0 Object Library
    3. 'Add one RichTextBox control (RichTextBox1) to your form
    4. Private Sub Command1_Click()
    5.    
    6.     Dim oApp As Word.Application
    7.     Dim oDoc As Word.Document
    8.     Dim oTable As Word.Table
    9.    
    10.     Set oApp = New Word.Application
    11.     Set oDoc = oApp.Documents.Open("C:\Development\Tips\Word FAQ\Word FAQ.doc")
    12.     Set oTable = oDoc.Tables.Item(1)
    13.     oTable.Select
    14.     oApp.Selection.Copy
    15.     RichTextBox1.TextRTF = Clipboard.GetText(vbCFRTF)
    16.     'Clean up and close doocument
    17.     Set oTable = Nothing
    18.     oDoc.Close SaveChanges:=False
    19.     Set oDoc = Nothing
    20.     oApp.Quit
    21.     Set oApp = Nothing
    22.    
    23. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: [FAQ's: OD] How do I add/modify/read a table programmatically?



    Word 2003 And VB 6 Code Example (Write Table):
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Word xx.0 Object Library
    3. Public Sub Write2Table()
    4.  
    5.     Dim oApp As Word.Application
    6.     Dim oDoc As Word.Document
    7.     Dim oTable As Word.Table
    8.     Dim x As Integer
    9.     Dim y As Integer
    10.     Dim z As Integer
    11.    
    12.     Set oApp = New Word.Application
    13.     'Open an exisiting document
    14.     Set oDoc = oApp.Documents.Open("C:\Document1.doc")
    15.     Set oTable = oDoc.Tables.Item(2) 'Second table for ex.
    16.     With oTable
    17.         z = 1
    18.         'Populate the data cells
    19.         For x = 2 To oTable.Rows.Count
    20.             For y = 1 To oTable.Columns.Count
    21.                 'Add cell color formatting
    22.                 .Cell(x, y).Range.Font.ColorIndex = z
    23.                 'Write to the table cell
    24.                 .Cell(x, y).Range.Text = "Meow " & x - 1
    25.                 z = z + 1
    26.             Next
    27.         Next
    28.     End With
    29.     'Clean up
    30.     Set oTable = Nothing
    31.     oDoc.Save
    32.     'Leave the document open for reviewing
    33.     Set oDoc = Nothing
    34.     Set oApp = Nothing
    35.  
    36. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width