Hello Experts!
I am working ona sub routine that will open a word template and fill an existing table with text from a dbase to create mailing labels...I know Crystal would be easier but I am doing it in Word! The template page I am using is the Avery 5261 (attached), which starts with a single sheet of labels, table columns are 4", .19" and 4".
Here is what I have so far.
Private Sub btnLabels_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLabels.Click
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oRng As Word.Range
Dim Pos As Double
'Start Word and open the label document template.
oWord = CreateObject("Word.Applica
tion")
oWord.Visible = True
oDoc = oWord.Documents.Open("c:\t
emp\labels
")
Dim rowcount as 23 'hardcoded for example purposes
rowcount = rowcount / 2 ' two labels per row
rowcount = Format(rowcount, "####") ' To make rows a whole number
Dim r As Integer = 1, c As Integer = 1
oTable = oDoc.Tables.Add(oDoc.Bookm
arks.Item(
"\endofdoc
").Range, rowcount, 3)
oTable.Range.ParagraphForm
at.SpaceAf
ter = 0
strsql 'to get data from database
Do While dbread.Read
If c = 2 Then
c = c + 1
End If
Dim lblatt = field1
Dim lbldist = field2
Dim lblstreet = field3
Dim lblcity = field4
Dim lblstate = field5
Dim lblzip = field6
If Len(lblzip) > 5 Then
Dim lblzip6 = lblzip.substring(0, 5)
Dim lblzip4 = lblzip.substring(5, 4)
lblzip = lblzip6 & "-" & lblzip4
End If
Dim lblline2 = lblcity & ", " & lblstate & " " & lblzip
oTable.Cell(r, c).Range.Text = lbldist & vbCrLf & lblatt & vbCrLf & lblstreet & vbCrLf & lblline2
c = c + 1
If c < 4 Then
r = r
End If
If c = 4 Then
c = 1
r = r + 1
End If
Loop
End Sub
This works EXCEPT...rather than use the template table for ALL labels, it only uese it for the first 20 and then goes to the table which is added in the code. That table is not formatted correctly.
I guess the question is instead of creating a new table at run time after opening the template, how do I identify the cells in the existing table so I can add more than 20 at run time and keep them formatted correctly?
I got my code examples from here:
http://support.microsoft.com/kb/316383Thanks!