View Single Post
 
Old 02-22-2014, 10:06 AM
marksm33 marksm33 is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Jan 2014
Posts: 12
marksm33 is on a distinguished road
Smile Multiple Userforms Displaying Different Content but Returning Same Content?

I believe I have 98% of this project completed. I have 2 MS Word Documents. One stores source tables, the other (the Main Document) is the document that the user interacts with. On the Main Document there are multiple macrobuttons. Each of these macrobuttons leads to a separate Userform. The userforms pull data from the tables on the second document. The user then selects data from the userform to insert into the Main Document at prescribed bookmarks that are delineated in the commandbutton code. Everything up until this point works PERFECTLY. However, no matter which userform the user selects information from, even though the userforms DISPLAY the correct data from different columns in the tables on the source document, the commandbutton inserts data ONLY FROM THE FIRST COLUMN of the FIRST TABLE. I feel like I am missing a very small but important step. I feel like the code is incorrect either in the individual Userforms or the Commandbuttons, but I'll be darned if I can understand where. I just need to know how to fix the problem, I can take it from there. I use MS Word 2010 with Windows 7.



Here is the code that I am using in the Userforms. I BELIEVE the problem is located somewhere in the underlined portion where it is generating an array.

Code:
Private Sub Userform_Initialize()

Dim oDoc As Word.Document
Dim oTbl As Word.Table
Dim arrTableData() As String
Dim lngRows As Long, lngCols As Long
Dim strSQL As String

Dim arrData() As String
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long

  'Load Lb2br using an external Word document.  Data is defined in a document table.
    'Open the document not visible to the user.
    Set oDoc = Documents.Open(ThisDocument.Path & "\Word Table Data Source.docm", , , , , , , , , , , False)
    Set oTbl = oDoc.Tables(2)
    
    'Get the number of list members (i.e., table rows - 1 if header row is used)
  i = oDoc.Tables(2).Rows.Count - 1
  'Get the number of list member attritbutes (i.e., table columns)
  j = oDoc.Tables(2).Columns.Count
  'Set the number of columns in the Listbox
  lb2br.ColumnCount = j
  'Load list members into an array
  ReDim arrData(i - 1, j - 1)
  For n = 0 To j - 1
    For m = 0 To i - 1
      Set myitem = oDoc.Tables(2).Cell(m + 2, n + 1).Range
      myitem.End = myitem.End - 1
      arrData(m, n) = myitem.Text
    Next m
  Next n
 

    'Use the .ColumnWidth property to set column widths.  0 results in a hidden column.
  lb2br.ColumnWidths = "0;800;0;0;0"
  
    'Use the .AddItem method to add a multi-column row for each array element.
    lb2br.AddItem

    
    
    'Dimesion a data array to collect contents. Note: .Row.Count - 2 is used to exclude the table heading row.
    ReDim arrTableData(oTbl.Rows.Count - 2, oTbl.Columns.Count - 1)
    'Strip end of cell marker and data from each row/column (cell) to array
    For lngRows = 2 To oTbl.Rows.Count
      For lngCols = 1 To oTbl.Columns.Count
        arrTableData(lngRows - 2, lngCols - 1) = Left(oTbl.Cell(lngRows, lngCols).Range.Text, _
                                                 Len(oTbl.Cell(lngRows, lngCols).Range.Text) - 2)
      Next lngCols
    Next lngRows
    'Populate ListBox with array data using the List method.
    lb2br.List = arrTableData

Reply With Quote