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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Table in MSWord9.0 from VB6.0 2

Status
Not open for further replies.

leishad

Technical User
Apr 3, 2003
88
US
From Visual Basic 6.0:

Create a document(word 9.0) that has a table (at the 17th line of the document with 4 columns) in it to receive a varying number of records (hence, I will build it dynamically with a loop of my recordset).

At this point I am just experimenting with the Word Objects as they are new to me. While my code may be worth ignoring completely at this point I have included my experimentation below. I think that I have definite issues understanding the use of selection and range:

I thought I was moving on the right track with this until I tryed to move the table down to line 17, and then my text no longer inserts in a cell but at the top of the document. Any assistance would be appreciated.
-----------------------------------------------------

Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim WordRange As Word.Range
Dim WordTable As Word.Table
Dim WordColumn As Word.Column
Dim WordRow As Word.Row

Private Sub Command1_Click()

Set WordObj = Nothing
Set WordObj = CreateObject("Word.Application")
Set WordDoc = WordObj.Documents.Open("C:\doc2.doc") 'Open the document
Set WordRange = WordDoc.Range(Start:=0, End:=0)
WordObj.Visible = True

With WordRange
.InsertBefore ("Document Statistics")
.Font.Name = "Veranda"
.Font.Size = 16
.InsertParagraphAfter
.InsertParagraphAfter
.SetRange Start:=WordRange.End, _
End:=WordRange.End
End With

WordDoc.Range(Start:=0, End:=0).Bold = True
Set WordTable = WordDoc.Tables.Add(WordDoc.Paragraphs.Item(2).Range, 2, 4, wdWord9TableBehavior, wdAutoFitFixed)
Set WordRow = WordTable.Rows.Add(BeforeRow:=WordTable.Rows(1))
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext
WordDoc.Tables(1).Cell(1, 1).Select
WordRange = Selection.Range

WordRange.Text = "test"
WordRange.Move Unit:=wdCell
WordRange.Text = "test"
WordRange.Move Unit:=wdCell
WordTable.Rows.Add BeforeRow:=WordTable.Rows(1)
WordRange.Move Unit:=wdRow
WordRange.Text = "test"
WordRange.Move Unit:=wdCell
WordRange.Text = "test"
End Sub
------------------------------------------------------------------------------------
 
I find it easier to send the data to word as a delimited string and then use the ConvertToTable function:
Code:
[COLOR=green]' Generate string, delimited with'$' (or whatever)[/color]
Do While Not rs.EOF
mystr = mystr & rs.Fields(1) & "$" & rs.Fields(2) & "$" & rs.Fields(3) & "$" & rs.Fields(1) & vbCrLf
rs.MoveNext
Loop
[COLOR=green]' Move to bookmark in document[/color]
Set myrange = mydoc.Bookmarks("list").Range
myrange.InsertAfter mystr
[COLOR=green]' Convert to table[/color]
With myrange
    .ConvertToTable Separator:="$", _
        NumColumns:=4, Format:=wdTableFormatGrid7, ApplyBorders:=True _
        , ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows _
        :=False, ApplyLastRow:=False, ApplyFirstColumn:=False, ApplyLastColumn:= _
        False, AutoFit:=True
End With

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Thank-you. That just seems so simple, yet I could not have come up with it at this point. While I still endeavor to fully 'get it' with word objects you have pulled me out of my stuck point - it works just as you described.
 
You're welcome - and thanks for the *

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Hi Johnwm

Thx a bunch - I have now been strugeling with the above for 2 days ..... and making very little progress :-(

Then I found your code and, allthough I still have a few problems to solve before its perfect, your code moved me to "the next level" ;-)

Herman

They say that crime doesn't pay... does that mean my job is a crime?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top