|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
The code you've posted initializes the userform with contents drawn only from the second table in the document. So, if your command button is doing something different, it seems to me your command button code is where you should look.
A couple of points: 1. Having set oTbl via 'Set oTbl = oDoc.Tables(2)', the first set of loops ignores that and continues to reference oDoc.Tables(2); 2. Whereas the first loop efficiently uses a range object to retrieve the cell content, the second loop takes an entirely different, and less efficient, approach, retrieving then truncating the cell content. You could quite easily have re-used the previous range object (and even the row/column indices rather than using a completely new set).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
multiple userforms |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Allow Multiple Content Control same cell of form table | DeborahBartlett | Word | 1 | 01-04-2014 11:06 AM |
Deleting a table from a content control -- preserving the content control | BrainSlugs83 | Word Tables | 8 | 11-14-2013 03:06 AM |
Content Controls | Sammie0Sue | Word | 6 | 11-06-2013 10:56 PM |
macro for change content of cell2 when content of cell1 changes | Intruder | Excel Programming | 1 | 11-17-2012 08:24 PM |
Package for CD with links to Windows media player content and Flash Shockwave content | hectorh | PowerPoint | 4 | 10-15-2009 12:22 PM |