Results 1 to 4 of 4

Thread: [RESOLVED] VB-Write in a Table using word

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    81

    Resolved [RESOLVED] VB-Write in a Table using word

    Hi,
    I want to create a table with just 1 row and 2 cols, each col has different values.
    When I try to write in that specific column, it just vanishes and writes only the last value.
    Please help
    for c=1 to 2
    If c = 2 Then


    oTable.Cell(1, c).Range.Text = "Customer Ship To: "

    oTable.Cell(1, c).Range.Text = "Test: "
    oApp.Selection.TypeParagraph
    end if
    Next
    it write only Test and not Customer Ship To

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: VB-Write in a Table using word

    Since nobody has replied.......I've never done macros in Word, but would guess that using the macro recorder would be very instructive. It certainly is in Excel.

  3. #3
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: VB-Write in a Table using word

    Unless I'm mistaken, your code is doing exactly what it should.

    The code only runs with c = 2, so the For statement seems pointless.
    With c = 2, the code will first add "Customer Ship To:" to cell(1,2) and then overwrite this value with "Test:" which is all you are left with.

    If you wanted to add headers to column 1 and 2, you could simply write:

    VB Code:
    1. Sub LoadTable()
    2.  
    3. Dim oTable As Table
    4. Dim TablePos As Range
    5.  
    6.     'This will insert your table at a bookmarked position named "Table1"
    7.     Set TablePos = ActiveDocument.Bookmarks("Table1").Range
    8.     Set oTable = ActiveDocument.Tables.Add(Range:=TablePos, _
    9.     NumRows:=1, NumColumns:=2)
    10.  
    11.         'Add first header
    12.         oTable.Cell(1, 1).Range.Text = "Customer Ship To: "
    13.         'Add second header
    14.         oTable.Cell(1, 2).Range.Text = "Test: "
    15.  
    16. End Sub

    Although it seems unecessary to write code for this in the first place.

    I suspect there is more to this than just adding 2 headers to a table? Exactly what are you trying to achieve?
    Last edited by New2vba; Apr 28th, 2006 at 04:43 PM. Reason: Modify code to insert table as well
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    81

    Re: VB-Write in a Table using word

    Thank you.

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