CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Creating Tables in MS Word

    Does anyone have a piece of code that shows how to create a table in word using vb code? I've gotten pretty good at formatting text in word from vb, but for my current project I would like to create a table and insert some values into the cells of that table from vb code. I've read the help files but they mainly deal with the formatting and obtaining of data in the table, not the actual creation of a table. Thanks in advance for any help!

    Jeff


  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Creating Tables in MS Word

    The easiest way I have found to do this is to Open word, turn macro recording on, create the table, stop the macro recording. Then use the editing functionality within word to copy the code and paste it into your VB Project. It should give you just what you need. This works for just about anything you need to do in word.

    Hope this helps.



  3. #3
    Join Date
    Jul 2001
    Posts
    1

    Re: Creating Tables in MS Word

    Jeff,
    I am pretty new with VB and just finished a project interfacing with Word. Here is the code I used, let me know if you have any questions.

    Don


    Dim oApp as Object
    Dim oDoc as Object
    Dim oTbl as Object
    Dim x as Integer

    'Start Word and open the document
    set oApp = CreateObject("word.Application")
    set oDoc = oApp.Documents.Open("C:\My Documents\test.doc")

    'make it Word Visible
    oApp.Visible = true

    'get Table Code_Typ description
    Dim str as string
    Dim rs as new ADODB.Recordset
    rs.ActiveConnection = cn

    'Select the initial Code Type
    str = "SELECT CODE_TYP, CODE_TYP_DESC FROM CODE_TYPES"
    rs.Open str

    Do While Not rs.EOF

    Dim dblRecCount as Double
    Dim rs1 as new ADODB.Recordset
    rs1.ActiveConnection = cn
    rs1.CursorType = adOpenStatic

    'set the font size
    oApp.Selection.Font.Size = 20

    'set Text Alignment
    oApp.Selection.ParagraphFormat.Alignment = 1

    'set the font to BOLD
    oApp.Selection.Font.Bold = true
    oApp.Selection.Font.Name = "Arial"

    'Type the text
    oApp.Selection.TypeText "Code Table Type Descriptions"
    Selection.InsertBreak Type:=wdLineBreak

    'set the font size
    oApp.Selection.Font.Size = 12

    str = rs("Code_Typ_Desc")
    Selection.TypeText str & "(" & rs("code_typ") & ")"
    Selection.InsertBreak Type:=wdLineBreakClearLeft

    'set the font size
    oApp.Selection.Font.Size = 10

    str = "SELECT CODE, CODE_TYPE, DESCRIPTION, Legacy_Value FROM CODES " & _
    "where CODE_TYPE = " & rs("code_typ") & _
    " order by code"
    rs1.Open str

    If Not rs1.EOF then
    rs1.MoveLast
    dblRecCount = rs1.RecordCount

    'set Number of Rows and Columns for insert of table (Add 1 for Column Heading)
    set oTbl = oDoc.Tables.Add(Selection.Range, rs1.RecordCount + 1, 3)

    rs1.MoveFirst
    Do While Not rs1.EOF
    With oTbl
    .Cell(1, 1).Shading.BackgroundPatternColorIndex = wdGray25
    .Cell(1, 1).Range.InsertAfter "Code Type"
    .Cell(1, 2).Shading.BackgroundPatternColorIndex = wdGray25
    .Cell(1, 2).Range.InsertAfter "Description"
    .Cell(1, 3).Shading.BackgroundPatternColorIndex = wdGray25
    .Cell(1, 3).Range.InsertAfter "Legacy Value"
    for x = 2 to dblRecCount + 1
    .Cell(x, 1).Range.Bold = false
    .Cell(x, 1).Range.InsertAfter rs1("CODE")
    .Cell(x, 2).Range.Bold = false
    .Cell(x, 2).Range.InsertAfter rs1("Description")
    .Cell(x, 3).Range.Bold = false
    .Cell(x, 3).Range.InsertAfter rs1("Legacy_Value")
    rs1.MoveNext
    next x
    End With
    Loop

    'Move to the end of the document before adding Line and Page Break
    Selection.EndKey Unit:=wdStory, Extend:=wdMove
    Selection.Range.InsertBreak Type:=wdLineBreak
    Selection.Range.InsertBreak Type:=wdPageBreak

    'Move to the end of the document
    Selection.EndKey Unit:=wdStory, Extend:=wdMove
    Selection.Range.InsertParagraphAfter
    else
    'set the Font back to BOLD
    oApp.Selection.Font.Bold = true
    oApp.Selection.Font.Name = "Arial"

    'set the font size
    oApp.Selection.Font.Size = 12

    'Move to the end of the document before adding Line Break
    Selection.EndKey Unit:=wdStory, Extend:=wdMove
    Selection.Range.InsertBreak Type:=wdLineBreak

    'Type the text
    oApp.Selection.TypeText ">> No Data Found for Code Type <<"

    'Add Page Break, then Paragraph, and then move to end of document
    Selection.Range.InsertBreak Type:=wdPageBreak
    Selection.Range.InsertParagraphAfter
    Selection.EndKey Unit:=wdStory, Extend:=wdMove

    End If
    rs1.Close
    rs.MoveNext
    Loop




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

Featured