vb and msword

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2006
Posts: 30
Reputation: skalra63 is an unknown quantity at this point 
Solved Threads: 0
skalra63 skalra63 is offline Offline
Light Poster

vb and msword

 
0
  #1
Apr 8th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 113
Reputation: Kegtapper is an unknown quantity at this point 
Solved Threads: 4
Kegtapper's Avatar
Kegtapper Kegtapper is offline Offline
Junior Poster

Re: vb and msword

 
0
  #2
Apr 25th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 30
Reputation: skalra63 is an unknown quantity at this point 
Solved Threads: 0
skalra63 skalra63 is offline Offline
Light Poster

Re: vb and msword

 
0
  #3
Apr 25th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC