Results 1 to 9 of 9

Thread: Problem inserting table to ms word using vb

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    15

    Problem inserting table to ms word using vb

    Anybody noe how to insert data into ms word table?
    thanks

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Problem inserting table to ms word using vb

    Originally posted by knic
    Anybody noe how to insert data into ms word table?
    thanks
    YEP!

    Word VBA help has lots of info on this topic. In MS Word press Alt + F11 (which will open the VBA editor). Then click help and search these topics:

    Recording a macro to generate code
    Working with tables
    InsertDatabase Method



  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If its a table that your creating on the fly, then just move to the cell you want by doing like this...

    Move up 2 rows.
    Application.Selection.MoveUp wdLine, 2

    Application.Selection.TypeText "This goes in the table cell."


    Change the column...
    Application.Selection.MoveRight wdCell, 1

    If your opening a previously created document then...
    Move down to the correct position and do the same...

    Application.Selection.MoveDown wdLine, 8

    Application.Selection.TypeText "This goes in the table cell."


  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Don't forget to add the reference to Word and set your word object.

  5. #5
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Or....you can take a few minutes to read up on the Working with tables help topic and learn how to deal directly with cells and ranges without the jittering, time-consuming, Selection.Move methods.

  6. #6
    Lively Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    70
    Not sure if you want to create the table youself, this is a section of some code I used to do this from a sql server.

    Dont forget the ref to word though

    VB Code:
    1. Set t = objdoc.Range(match.FirstIndex, match.FirstIndex + match.Length)
    2.        
    3.         Dim myTable As Word.Table
    4.        
    5.         Set myTable = objdoc.Tables.Add(t, Rows, cols)
    6.        
    7.         With myTable
    8.             For y = 1 To cols
    9.                 .Cell(1, y).Range.InsertAfter notNull(rs.Fields(y - 1).Name)
    10.             Next y
    11.             For x = 2 To Rows
    12.                 For y = 1 To cols
    13.                     .Cell(x, y).Range.InsertAfter notNull(rs(y - 1))
    14.                 Next y
    15.                 rs.MoveNext
    16.             Next x
    17.            
    18.             .Columns.AutoFit
    19.             .AutoFormat wdTableFormatProfessional
    20.             .Select
    21.         End With

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    15
    thanks
    i have learnt how to add the table now
    but i have a problem again
    how to add a image at the top of the document
    because i need to generate a report from the ms word
    so i need to insert a company logo there

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try this...
    Code:
    Application.Selection.InlineShapes.AddPicture "D:\My Documents\03Cobra.jpg", False, True

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    15
    thanks
    now i can add the picture
    but how to adjust the size of the pic?

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