i have the following code, however, the table that is created always starts at the begining of the document. so none of the text written beforehand can be seen. i also dont know how to write things after the table.

Private Sub wrd2()
Dim c As Long
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim wrdTbl As Word.Table
Dim wrdRange As Word.Range

    Set wrdApp = New Word.Application
    Set wrdDoc = wrdApp.Documents.Add
    Set wrdRange = wrdDoc.Range(Start:=0, End:=0)
        r = flxItems.Rows
        c = flxItems.Cols
    Set wrdTbl = wrdDoc.Tables.Add(wrdRange, r, c)
    With wrdApp
        .Visible = True
        .ActiveDocument.SaveAs App.Path & "\invoices\" & invoiceID & ".doc"
        .Selection.Font.Name = "Arial"
        .Selection.Font.Size = 12
        .Selection.TypeText Text:="Customer Name :  " & txtCustomerName & vbNewLine
        .Selection.TypeText Text:="Staff ID :  " & lblStaffID & vbNewLine
        .Selection.TypeText Text:="Invoice Date :  " & currDate & vbNewLine
        .selectionTypeText Text:="Invoice Number :  " & invoiceID & vbNewLine & vbNewLine & vbNewLine
    End With
If flxItems.Rows = 0 Or flxItems.Rows = 1 Then Exit Sub
    r = flxItems.Rows
    c = flxItems.Cols
    With flxItems
        For r = 1 To .Rows
            For c = 1 To .Cols
                wrdTbl.Cell(r, c).Range.Text = .TextMatrix(r - 1, c - 1)
            Next c
        Next r
    End With
    With wrdTbl
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    End With
    wrdApp.ActiveDocument.Save
    wrdApp.Quit False
End Sub

anyone willing and able to help?

Recommended Answers

All 2 Replies

There are some issues that you need to address to position the TABLE in MSWord from VB. You'd have to enumerate the Table and

Public Enum as wdTablePosition (or something like that)

Also you can try this. Because you didn't include other TAGS some items were changed so that I could get it to work.

Private Sub wrd2()

Dim InvoiceID As Integer  '*********Added Code **************
Dim r As Long                  '********was missing from my copy********
Dim c As Long
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim wrdTbl As Word.Table
Dim wrdRange As Word.Range

InvoiceID = 7755          '*********Added Code to have an Inv ID# **************
    Set wrdApp = New Word.Application
    Set wrdDoc = wrdApp.Documents.Add
    Set wrdRange = wrdDoc.Range(Start:=0, End:=0)
    r = InputBox("Enter Number of Rows", "Row Count")   '***Added Code to get a ROW*****
    c = InputBox("Enter Number of Columns", "Columns") '***Added Codeto get a COL******
        'r = flxItems.Rows
        'c = flxItems.Cols

    With wrdApp
        .Visible = True
        .ActiveDocument.SaveAs App.Path & "\invoices\" & InvoiceID & ".doc"
        .Selection.Font.Name = "Arial"
        .Selection.Font.Size = 12
        .Selection.TypeText Text:="Customer Name :  " & "JOE" & vbNewLine  'txtCustomerName & vbNewLine
        .Selection.TypeText Text:="Staff ID :  " & "777777" & vbNewLine  ' lblStaffID & vbNewLine
        .Selection.TypeText Text:="Invoice Date :  " & Format(Now(), "mmm dd yyyy") & vbNewLine 'currDate & vbNewLine
        .Selection.TypeText Text:="Invoice Number :  " & InvoiceID & vbNewLine & _
         vbNewLine & vbNewLine '*********Corrected Code had a period missing **************
         .Selection.EndOf wdParagraph, wdExtend
    End With
        
        
        Set wrdTbl = wrdDoc.Tables.Add(wrdRange, r, c)
If wrdTbl.Rows.count = 0 Or wrdTbl.Rows.count = 1 Then Exit Sub
'flxItems.Rows = 0 Or flxItems.Rows = 1 Then Exit Sub
    r = wrdTbl.Rows.count '****Corrected Code (no flxItems here)*******  'flxItems.Rows
    c = wrdTbl.Columns.count '***Corrected Code  (no flxItems here)****  'flxItems.Cols
    With wrdTbl
        
        For r = 1 To .Rows.count
            For c = 1 To .Columns.count
                wrdTbl.Cell(r, c).Range.Text = "this text" ' ***(No .TextMatrix here) > .TextMatrix(r - 1, c - 1)
            Next c
        Next r
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    End With
    'With wrdTbl
    '    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    '    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    '    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    '    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    'End With
    wrdApp.ActiveDocument.Save
    wrdApp.Quit False
End Sub

This puts the ID/NAME/INVOICEID information at the bottom of the table.

You were lucky I was working on some Word/Excel/VB6 interoperability issues this week.
Because I am using Word2000, I didn't have some of the options.. such as .TextMatrix etc

Hope that helps somewhat

ah thanks, i meant to say i solved the problem but i must have forgot.
in the end i was able to get the company information to display top centre, the invoice details underneath, to the right, the list of items in the centre and the payment information at the bottom on the right.

what i had to do was change the following:
on the table, instead of assigning the actual value of Range. using the following code, anything text can be inputted, followed by the table. i used this.

Set wrdRange = wrdApp.Selection.Range
        r = flxItems.Rows
        c = flxItems.Cols
    Set wrdTbl = wrdDoc.Tables.Add(wrdRange, r, c)
If flxItems.Rows = 0 Or flxItems.Rows = 1 Then Exit Sub
    r = flxItems.Rows
    c = flxItems.Cols
    With flxItems
        For r = 1 To .Rows
            For c = 1 To .Cols
                wrdTbl.Cell(r, c).Range.Text = .TextMatrix(r - 1, c - 1)
            Next c
        Next

this code was used to navigate to the final cell and go to the next line. allowing tet to be inputted after the table.

wrdApp.Selection.MoveRight Unit:=wdWord, Count:=7, Extend:=wdExtend  'move right to 7th column (outside the table)
    wrdApp.Selection.MoveDown Unit:=wdLine, Count:=r + 1 'go to last row
    wrdApp.Selection.MoveDown Unit:=wdLine ' got to next line

thanks for replying though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.