Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to enter a table with 2 columns and 6 rows in a word doc

Status
Not open for further replies.

myrgir

Programmer
Feb 7, 2006
62
CA
hi
I would like to be able to enter a table in a word document. I tried this:
Code:
With wrd.Selection
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
            End With
            .Tables.Add(wrd.Selection.Range, 6, 3)
But After that I put text and all my text go in the first column and first row. So how can I enter the text that I want in a specific row/column?
Thanks
 
A starting point:
With wrd.Selection
With .Font
.Name = "Arial"
.Size = 12
.Bold = False
End With
Set tableNew = .Tables.Add(.Range, 6, 3)
With tableNew
.Cell(rowNum, colNum).Range.InsertAfter "Your text here"
...
End With
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Be aware of the difference between using .InsertAfter, and .Text. It may make a difference for you...or not.

.Cell(rowNum, colNum).Range.InsertAfter "Your text here"

will of course always add the inserted text after what is already there. If a space is required between the inserted text and existing text, you must put it in.

If the cell is blank,

.Cell(rowNum, colNum).Range.Text = "Your text here"

and

.Cell(rowNum, colNum).Range.InsertAfter "Your text here"

have the equivalent effect. Using .Text though will always replace the existing contents of the cell. InsertAfter always appends.

Gerry
 
Dim TableNew As Word.Table

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
And if you use late binding:
Dim TableNew As Object

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Ok I tried it
Code:
...
Case "945" '2.15.9.9.5
    SimpleInsertTable(StringDr("Message"))
...

                    
Private Sub SimpleInsertTable(ByVal sText As String)
        Dim tablenew As Word.Table
        With wrd.Selection
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
            End With
            tableNew = .Tables.Add(.Range, 6, 3)
            With tableNew
                .Cell(1, 2)range.InsertAfter(sText)
            End With
        End With
    End Sub
It put a table and it insert my text in the second colum/first row. But it insert text from each other Case after the case "945" in the first column/first row until the end of my document, how can I tell the text after case 945 for example, shouldn't be in the table?
Thanks
 
you may try this:
Private Sub SimpleInsertTable(ByVal sText As String)
Dim tablenew As Word.Table
With wrd.Selection
With .Font
.Name = "Arial"
.Size = 12
.Bold = False
End With
[highlight]Set[/highlight] tableNew = .Tables.Add(.Range, 6, 3)
With tableNew
.Cell(1, 2)[highlight].[/highlight]Range.InsertAfter sText
End With
[highlight].EndKey Unit:=wdStory[/highlight]
End With
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
If you use early binding then wdStory is already defined.
If you use late binding:
Private Sub SimpleInsertTable(ByVal sText As String)
Dim tablenew As [!]Objext[/!]
With wrd.Selection
With .Font
.Name = "Arial"
.Size = 12
.Bold = False
End With
Set tableNew = .Tables.Add(.Range, 6, 3)
With tableNew
.Cell(1, 2).Range.InsertAfter sText
End With
.EndKey Unit:=[!]6 ' 6=wdStory[/!]
End With
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
what do you mean by late binding or early binding?
It tell me that wdStory is not declared
 
You don't declare wdStory. Try googling "late binding".

This is an important piece of knowledge.
how can I tell the text after case 945 for example, shouldn't be in the table?
Case 945 moves the Selection. Any other code that uses the Selection afterwards will of course start from the last location of the Selection. In this case, it is in the table.

You can, as PH suggest move the Selection to the end of the document. That is what EndKey Unit:-wdStory will do. It moves the Selection to the end of the document.

That however, may not be where you want it. Far better to move it explicitly to where you want it. Unless of course that IS where you want it.

Far better yet - don't use the Selection at all.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top