Results 1 to 4 of 4

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

Threaded View

  1. #1

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

    [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

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