944,137 Members | Top Members by Rank

Ad:
Apr 8th, 2006
0

vb and msword

Expand Post »
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.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub wrd2()
  2. Dim c As Long
  3. Dim wrdApp As Word.Application
  4. Dim wrdDoc As Word.Document
  5. Dim wrdTbl As Word.Table
  6. Dim wrdRange As Word.Range
  7.  
  8. Set wrdApp = New Word.Application
  9. Set wrdDoc = wrdApp.Documents.Add
  10. Set wrdRange = wrdDoc.Range(Start:=0, End:=0)
  11. r = flxItems.Rows
  12. c = flxItems.Cols
  13. Set wrdTbl = wrdDoc.Tables.Add(wrdRange, r, c)
  14. With wrdApp
  15. .Visible = True
  16. .ActiveDocument.SaveAs App.Path & "\invoices\" & invoiceID & ".doc"
  17. .Selection.Font.Name = "Arial"
  18. .Selection.Font.Size = 12
  19. .Selection.TypeText Text:="Customer Name : " & txtCustomerName & vbNewLine
  20. .Selection.TypeText Text:="Staff ID : " & lblStaffID & vbNewLine
  21. .Selection.TypeText Text:="Invoice Date : " & currDate & vbNewLine
  22. .selectionTypeText Text:="Invoice Number : " & invoiceID & vbNewLine & vbNewLine & vbNewLine
  23. End With
  24. If flxItems.Rows = 0 Or flxItems.Rows = 1 Then Exit Sub
  25. r = flxItems.Rows
  26. c = flxItems.Cols
  27. With flxItems
  28. For r = 1 To .Rows
  29. For c = 1 To .Cols
  30. wrdTbl.Cell(r, c).Range.Text = .TextMatrix(r - 1, c - 1)
  31. Next c
  32. Next r
  33. End With
  34. With wrdTbl
  35. .Borders(wdBorderTop).LineStyle = wdLineStyleNone
  36. .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  37. .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  38. .Borders(wdBorderRight).LineStyle = wdLineStyleNone
  39. End With
  40. wrdApp.ActiveDocument.Save
  41. wrdApp.Quit False
  42. End Sub
anyone willing and able to help?
Similar Threads
Reputation Points: 13
Solved Threads: 0
Light Poster
skalra63 is offline Offline
30 posts
since Feb 2006
Apr 25th, 2006
0

Re: vb and msword

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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub wrd2()
  2.  
  3. Dim InvoiceID As Integer '*********Added Code **************
  4. Dim r As Long '********was missing from my copy********
  5. Dim c As Long
  6. Dim wrdApp As Word.Application
  7. Dim wrdDoc As Word.Document
  8. Dim wrdTbl As Word.Table
  9. Dim wrdRange As Word.Range
  10.  
  11. InvoiceID = 7755 '*********Added Code to have an Inv ID# **************
  12. Set wrdApp = New Word.Application
  13. Set wrdDoc = wrdApp.Documents.Add
  14. Set wrdRange = wrdDoc.Range(Start:=0, End:=0)
  15. r = InputBox("Enter Number of Rows", "Row Count") '***Added Code to get a ROW*****
  16. c = InputBox("Enter Number of Columns", "Columns") '***Added Codeto get a COL******
  17. 'r = flxItems.Rows
  18. 'c = flxItems.Cols
  19.  
  20. With wrdApp
  21. .Visible = True
  22. .ActiveDocument.SaveAs App.Path & "\invoices\" & InvoiceID & ".doc"
  23. .Selection.Font.Name = "Arial"
  24. .Selection.Font.Size = 12
  25. .Selection.TypeText Text:="Customer Name : " & "JOE" & vbNewLine 'txtCustomerName & vbNewLine
  26. .Selection.TypeText Text:="Staff ID : " & "777777" & vbNewLine ' lblStaffID & vbNewLine
  27. .Selection.TypeText Text:="Invoice Date : " & Format(Now(), "mmm dd yyyy") & vbNewLine 'currDate & vbNewLine
  28. .Selection.TypeText Text:="Invoice Number : " & InvoiceID & vbNewLine & _
  29. vbNewLine & vbNewLine '*********Corrected Code had a period missing **************
  30. .Selection.EndOf wdParagraph, wdExtend
  31. End With
  32.  
  33.  
  34. Set wrdTbl = wrdDoc.Tables.Add(wrdRange, r, c)
  35. If wrdTbl.Rows.count = 0 Or wrdTbl.Rows.count = 1 Then Exit Sub
  36. 'flxItems.Rows = 0 Or flxItems.Rows = 1 Then Exit Sub
  37. r = wrdTbl.Rows.count '****Corrected Code (no flxItems here)******* 'flxItems.Rows
  38. c = wrdTbl.Columns.count '***Corrected Code (no flxItems here)**** 'flxItems.Cols
  39. With wrdTbl
  40.  
  41. For r = 1 To .Rows.count
  42. For c = 1 To .Columns.count
  43. wrdTbl.Cell(r, c).Range.Text = "this text" ' ***(No .TextMatrix here) > .TextMatrix(r - 1, c - 1)
  44. Next c
  45. Next r
  46. .Borders(wdBorderTop).LineStyle = wdLineStyleNone
  47. .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  48. .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  49. .Borders(wdBorderRight).LineStyle = wdLineStyleNone
  50. End With
  51. 'With wrdTbl
  52. ' .Borders(wdBorderTop).LineStyle = wdLineStyleNone
  53. ' .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
  54. ' .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
  55. ' .Borders(wdBorderRight).LineStyle = wdLineStyleNone
  56. 'End With
  57. wrdApp.ActiveDocument.Save
  58. wrdApp.Quit False
  59. 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
Reputation Points: 13
Solved Threads: 4
Junior Poster
Kegtapper is offline Offline
115 posts
since Oct 2005
Apr 25th, 2006
0

Re: vb and msword

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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Set wrdRange = wrdApp.Selection.Range
  2. r = flxItems.Rows
  3. c = flxItems.Cols
  4. Set wrdTbl = wrdDoc.Tables.Add(wrdRange, r, c)
  5. If flxItems.Rows = 0 Or flxItems.Rows = 1 Then Exit Sub
  6. r = flxItems.Rows
  7. c = flxItems.Cols
  8. With flxItems
  9. For r = 1 To .Rows
  10. For c = 1 To .Cols
  11. wrdTbl.Cell(r, c).Range.Text = .TextMatrix(r - 1, c - 1)
  12. Next c
  13. 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.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. wrdApp.Selection.MoveRight Unit:=wdWord, Count:=7, Extend:=wdExtend 'move right to 7th column (outside the table)
  2. wrdApp.Selection.MoveDown Unit:=wdLine, Count:=r + 1 'go to last row
  3. wrdApp.Selection.MoveDown Unit:=wdLine ' got to next line

thanks for replying though.
Reputation Points: 13
Solved Threads: 0
Light Poster
skalra63 is offline Offline
30 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Massage box in VB
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Huge flat file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC