-
Apr 27th, 2006, 02:16 AM
#1
[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:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
Public Function AddTable(ByVal iRows As Integer, ByVal iCols As Integer) As Boolean
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oRowHeader As Word.Row
Dim x As Integer
Set oApp = New Word.Application
'Open either a blank new document or...
Set oDoc = oApp.Documents.Add
'open an exisiting document
'Set oDoc = oApp.Documents.Open("C:\Document1.doc")
oDoc.Activate
'Move to the end of the document
oApp.Selection.Move Unit:=wdStory
oApp.Selection.TypeParagraph
oApp.Selection.TypeParagraph
'Add a new table
Set oTable = oDoc.Tables.Add(Range:=Selection.Range, NumRows:=iRows, NumColumns:=iCols, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow)
'Format the table and populate
With oTable
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
'Populate and format the table header
Set oRowHeader = oTable.Rows.Item(1)
oRowHeader.Shading.Texture = wdTextureNone
oRowHeader.Shading.ForegroundPatternColor = wdColorAutomatic
oRowHeader.Shading.BackgroundPatternColor = wdColorGray25
For x = 1 To iCols
'Bold on for the cell
.Cell(1, x).Range.Font.Bold = True
.Cell(1, x).Range.Text = "ColumnHeader" & x
Next
End With
'Clean up and leave document open
Set oRowHeader = Nothing
Set oTable = Nothing
Set oDoc = Nothing
Set oApp = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Aug 23rd, 2006, 04:20 PM
#2
Re: [FAQ's: OD] How do I add/modify/read a table programmatically?
Word 2003 And VB 6 Code Example (Read Table):
VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
'Add a MS Flexgrid control and a Command Button.
Private Sub Command4_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim x As Integer
Dim y As Integer
Set oApp = New Word.Application
'open an exisiting document
Set oDoc = oApp.Documents.Open("C:\Document1.doc")
Set oTable = oDoc.Tables.Item(1)
With oTable
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.Cols = .Columns.Count
MSFlexGrid1.Rows = .Rows.Count
MSFlexGrid1.Clear
'Set up header row
For y = 0 To .Columns.Count - 1
MSFlexGrid1.Row = 0
MSFlexGrid1.Col = y
'Trim off the carriage return (chr 13 and 7)
MSFlexGrid1.Text = Replace(.Cell(1, y + 1).Range.Text, Chr(13) & Chr(7), vbNullString)
Next
'Read the cells data into the ms flexgrid control
For x = 1 To .Rows.Count - 1
For y = 0 To .Columns.Count - 1
MSFlexGrid1.Col = y
MSFlexGrid1.Row = x
MSFlexGrid1.Text = Replace(.Cell(x + 1, y + 1).Range.Text, Chr(13) & Chr(7), vbNullString)
Next
Next
End With
'Clean up and close document
Set oTable = Nothing
oDoc.Close SAveChanges:=False
Set oDoc = Nothing
oApp.Quit
Set oApp = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Aug 23rd, 2006, 04:21 PM
#3
Re: [FAQ's: OD] How do I add/modify/read a table programmatically?
Word 2003 And VB 6 Code Example (Read Table):
VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
'Add one RichTextBox control (RichTextBox1) to your form
Private Sub Command1_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Open("C:\Development\Tips\Word FAQ\Word FAQ.doc")
Set oTable = oDoc.Tables.Item(1)
oTable.Select
oApp.Selection.Copy
RichTextBox1.TextRTF = Clipboard.GetText(vbCFRTF)
'Clean up and close doocument
Set oTable = Nothing
oDoc.Close SaveChanges:=False
Set oDoc = Nothing
oApp.Quit
Set oApp = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Aug 23rd, 2006, 04:22 PM
#4
Re: [FAQ's: OD] How do I add/modify/read a table programmatically?
Word 2003 And VB 6 Code Example (Write Table):
VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
Public Sub Write2Table()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim x As Integer
Dim y As Integer
Dim z As Integer
Set oApp = New Word.Application
'Open an exisiting document
Set oDoc = oApp.Documents.Open("C:\Document1.doc")
Set oTable = oDoc.Tables.Item(2) 'Second table for ex.
With oTable
z = 1
'Populate the data cells
For x = 2 To oTable.Rows.Count
For y = 1 To oTable.Columns.Count
'Add cell color formatting
.Cell(x, y).Range.Font.ColorIndex = z
'Write to the table cell
.Cell(x, y).Range.Text = "Meow " & x - 1
z = z + 1
Next
Next
End With
'Clean up
Set oTable = Nothing
oDoc.Save
'Leave the document open for reviewing
Set oDoc = Nothing
Set oApp = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|