Using word Object Library in VB

Posted on 2003-11-13
Last Modified: 2010-05-03
I have write some text in Word docuemnt using range object. After this I had added a table in to that docuement.  And again inserted some text. But the table is reside at end of docuemnt. why?
How I can insert a table in a Word document?
Question by:Dhanish
LVL 53

Accepted Solution

Dhaest earned 63 total points
ID: 9745912
Dim wdApp As New Word.Application
Dim wdDoc As New Word.Document
Dim wdTable As Word.Table
Dim saveas As String

wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add

With wdApp.Selection
    .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft

    Set wdTable = wdDoc.Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=4)

' change the layout of the table
    with wdTable
                .Borders.Enable = True
                .Cell(1, 1).Range.Font.Bold = True
                .Cell(1, 1).Width = wdApp.InchesToPoints(0.75)
                .Cell(1, 1).Range.Text = "Date:"
                .Cell(1, 2).Range.Font.Bold = False
                .Cell(1, 2).Width = wdApp.InchesToPoints(2)
                .Cell(1, 2).Range.Text = date1.Text
                .Cell(1, 3).Range.Font.Bold = True
                .Cell(1, 3).Width = wdApp.InchesToPoints(0.75)
                .Cell(1, 3).Range.Text = "Name:"
                .Cell(1, 4).Range.Font.Bold = False
                .Cell(1, 4).Width = wdApp.InchesToPoints(2)
                .Cell(1, 4).Range.Text = Name1.Text
                .Cell(2, 1).Range.Font.Bold = True
                .Cell(2, 1).Width = wdApp.InchesToPoints(0.75)
                .Cell(2, 1).Range.Text = "Dept:"
                .Cell(2, 2).Range.Font.Bold = False
                .Cell(2, 2).Width = wdApp.InchesToPoints(2)
                .Cell(2, 2).Range.Text = TextBox3.Text
                .Cell(2, 3).Width = wdApp.InchesToPoints(0.75)
                .Cell(2, 3).Range.Font.Bold = True
                .Cell(2, 3).Range.Text = "Location:"
                .Cell(2, 4).Range.Font.Bold = False
                .Cell(2, 4).Width = wdApp.InchesToPoints(2)
                .Cell(2, 4).Range.Text = TextBox5.Text
end with

Assisted Solution

atp_expert earned 62 total points
ID: 9747498
The reason of this is that, when the table is drawn, the cursor resides at the first cell of the table. What u need to do is.. get the cursor end of that table and get the cursor to the next line to the table and start writing. By this way ur table will remain at the proper position.
LVL 49

Expert Comment

ID: 10548299
Moderator, my recommended disposition is:

    Split points between: Dhaest & atp_expert

Dan Rollins -- EE database cleanup volunteer

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vba find the last empty column 10 88
Access 2003, my txt file export is running backwards 4 46
VB6 Compile Compatibility Issue 4 90
Help me. 3 46
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now