Results 1 to 11 of 11

Thread: **RESOLVED** Need Help With Adding Multiple Tables Into Word From Vb??

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679

    **RESOLVED** Need Help With Adding Multiple Tables Into Word From Vb??

    I need to know how I can write the code for multiple tables into Word...

    The code I have is as follows:
    VB Code:
    1. On Error GoTo Error_Handler
    2.        
    3.  Dim intNumofRows As Integer
    4.  Dim intNumofColumns As Integer
    5.  Dim p As Integer, ColWidth As Integer
    6.  Dim i As Integer
    7.  
    8.  
    9.  
    10.     intNumofColumns = m_Recordset.Fields.Count
    11.     intNumofRows = m_Recordset.RecordCount
    12.  
    13.     'Insert a new table with rows according to recordCount plus Column header
    14.     'and the number of columns in the recordset
    15.    
    16.    
    17.      
    18.     wrdDoc.Tables.Add wrdSelection.Range, NumRows:=intNumofRows + 1, _
    19.     NumColumns:=intNumofColumns
    20.    
    21.     iTableCounter = iTableCounter + 1
    22.    
    23.    
    24.    
    25.     With wrdDoc.Tables(iTableCounter)
    26.     ' Set the column widths
    27.      For i = 0 To intNumofColumns - 1
    28.      ColWidth = Len(m_Recordset.Fields(i).Name)
    29.         .Columns(i + 1).SetWidth ColWidth * 25, wdAdjustNone
    30.         .Cell(1, i + 1).Range.InsertAfter UCase(m_Recordset.Fields(i).Name)
    31.      Next i
    32.        
    33.         ' Set the shading on the first row to light gray
    34.         .Rows(1).Cells.Shading.BackgroundPatternColorIndex = wdGray25
    35.        
    36.         ' Bold the first row
    37.         .Rows(1).Range.Bold = True
    38.        
    39.         ' Center the text in Cell (1,1)
    40.         .Cell(1, 1).Range.Paragraphs.Alignment = wdAlignParagraphCenter
    41.        
    42.         ' Fill each row of the table with data
    43.         For i = 1 To intNumofRows
    44.          For p = 1 To intNumofColumns
    45.           FillRow i + 1, p, m_Recordset.Fields(p - 1)
    46.          Next p
    47.          p = 1
    48.          m_Recordset.MoveNext
    49.         Next i
    50.     End With
    51.        
    52.    Set m_Recordset = Nothing
    53.    
    54.    
    55.    
    56.    
    57.   Exit Sub
    58.    
    59. Error_Handler:
    60.          
    61.     Debug.Print Err.Description & " - " & Err.Number
    This routine will be used to insert tables...the problem is that the liine of code ' wrdDoc.Tables.Add wrdSelection.Range' is being used more than once...When the 2nd table is being sent to the Word object the Debug.Print is saying that there is all ready a table in that location...

    I need to find the code to have multiple table on the same document????

    Thanks
    Last edited by Salvatore; Apr 26th, 2004 at 04:01 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Your on the same selection range. You need to move to a
    different location. Try something like ...
    VB Code:
    1. Application.Selection.TypeParagraph
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Hey RobDog, thanks for the reply...I myself am only on a mission of curiosity here that seems to be getting me more and more obsessed (you know, that "damn it I have to solve this riddle!" thing)....I have been playing around with this Word problem and looking at exactly what you referred to, in fact this is what I thought to be the initial problem all along. I tried inserting paragraphs, line breaks, anything that would move the selection. The real problem here is I don't know beans about VBA for Word, and the code that creates the tables in the attached project was grabbed from pscode.com site and basically, I do not fully understand it. Case in point:

    VB Code:
    1. wrdDoc.Tables.Add wrdApp.Selection.Range, NumRows:=intNumofRows + 1, _
    2.  NumColumns:=intNumofColumns
    3.    ' , DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    4.     '        wdAutoFitFixed

    In the above statement, wrdApp is a valid Application object. The commented bits are additional arguments I have been adding in attempts to get it to work. If you open the project, you will see in two places where I tried to move the selection, and when I step through, it moves no problem. But when attempting to populate the second table, I believe the above statement is the culprit. What is a Range object? Is this argument in the Tables.Add method doing two things? - providing a location for the table AND a unique index for the collection? The code actually crashes a couple of lines later when attempting to reference the second element in the Tables collection which it says does not exist - meaning that when it was added, it must have been added to the same index as the first table (it does not crash on the Add statement). When I pass the mouse over wrdApp.Selection.Range when stepping through, it comes up as an empty string ("") every time. Pretty soon, I'm gonna need a rubber room...

    Any thoughts would be much obliged.
    Attached Files Attached Files
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This applies to all areas of Word that yuo want to know how to
    program, open a new document and record a macro in
    ThisDocument of what you are trying to accomplish. Then look at
    the macro code and translate to vb.

    If I have some time I will lok at your project, but it should be a
    separate thread.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Finally found the solution (no need to look at project RobDog - thanks though anyway)...The selection object was not moving past the end of the tables until I did this:

    VB Code:
    1. wrdSelection.EndKey Unit:=wdStory

    Salvatore - place the above line at the end of the InsertTable WithData sub after it generates the table. It moves the cursor and changes the co-ordinates of the Range property. I think there is a lot of potential here....you should be able to format your reports to look as good as something like Crystal, but with much less hassle - plus you have a saveable copy you can e-mail or do whatever with. I'm thinking of incorporating something like it in my current app. It's been fun!!
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I think you can also do a
    VB Code:
    1. .Selection.MoveDown wdLine 'or wdParagraph
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Ahara,

    I placed the line of code that you found to solve this issue, but when I ran the code, the result was that it created 1 table with all the data from the 6 listviews.....

    I walked through the code and saw that within the InserTableWithData routine it was placing all the rows and columns into Table (1)...

    VB Code:
    1. On Error GoTo Error_Handler
    2.        
    3.  Dim intNumofRows As Integer
    4.  Dim intNumofColumns As Integer
    5.  Dim p As Integer, ColWidth As Integer
    6.  Dim i As Integer
    7.  
    8.  
    9.  
    10.     intNumofColumns = m_Recordset.Fields.Count
    11.     intNumofRows = m_Recordset.RecordCount
    12.  
    13.     'Insert a new table with rows according to recordCount plus Column header
    14.     'and the number of columns in the recordset
    15.      
    16.     wrdDoc.Tables.Add wrdSelection.Range, NumRows:=intNumofRows + 1, _
    17.     NumColumns:=intNumofColumns
    18.    
    19.     With wrdDoc.Tables(1) ' here is what I am referring to
    20.     ' Set the column widths
    21.      For i = 0 To intNumofColumns - 1
    22.      ColWidth = Len(m_Recordset.Fields(i).Name)
    23.         .Columns(i + 1).SetWidth ColWidth * 25, wdAdjustNone
    24.         .Cell(1, i + 1).Range.InsertAfter UCase(m_Recordset.Fields(i).Name)
    25.      Next i
    26.        
    27.         ' Set the shading on the first row to light gray
    28.         .Rows(1).Cells.Shading.BackgroundPatternColorIndex = wdGray25
    29.        
    30.         ' Bold the first row
    31.         .Rows(1).Range.Bold = True
    32.        
    33.         ' Center the text in Cell (1,1)
    34.         .Cell(1, 1).Range.Paragraphs.Alignment = wdAlignParagraphCenter
    35.        
    36.         ' Fill each row of the table with data
    37.         For i = 1 To intNumofRows
    38.          For p = 1 To intNumofColumns
    39.           FillRow i + 1, p, m_Recordset.Fields(p - 1)
    40.          Next p
    41.          p = 1
    42.          m_Recordset.MoveNext
    43.         Next i
    44.     End With
    45.        
    46.    Set m_Recordset = Nothing
    47.    
    48.    wrdSelection.EndKey Unit:=wdStory
    49.   Exit Sub
    50.    
    51. Error_Handler:
    52.          
    53.     Debug.Print Err.Description & " - " & Err.Number
    Did you get it to work with multiple tables?

    Thanks

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    I can see that you got it to work...I was too quick with the last reply...yet I still am getting a freaky result!

    I have the code launched from the toolbar like so:
    VB Code:
    1. Select Case mdiMaster.ActiveForm.Name
    2.         Case Is = "frmPrfmSmmry"
    3.              If ButtonKey = "tbExport" Then
    4.                 Set wrdDoc = wrdApp.Documents.Add
    5.                 wrdDoc.Select
    6.                 Set wrdSelection = wrdApp.Selection
    7.                 Dim ctl As Control
    8.                
    9.                 For Each ctl In frmPrfmSmmry.Controls
    10.                     If TypeOf ctl Is ListView Then
    11.                         Set rsQuery = getMultiValues(ctl, "*")
    12.                         Call InsertTableWithData(rsQuery)
    13.                         wrdSelection.InsertBreak Type:=wdPageBreak
    14.                     End If
    15.                 Next
    16.                
    17.                  
    18.                 wrdDoc.SaveAs App.Path & "\Summary.doc"
    19.                 wrdDoc.Close
    20.            
    21.                 MsgBox "File created"
    22.             End If
    So I have it look at the activeform within the MIDI, and if the frmPfrmSmmry is active then check which toolbar button was selected...

    So then it should look at the listviews within that form to fill the tables that are sent to the Word...but I am only getting 1 table with the columns filled with 6 instances of the data?????

  9. #9
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    You need a variable to reference the tables which gets incremented with each iteration....this is why you have only one table. In the end, the key was all about the selection object. Inserting a line or a paragraph does no good if the selection is not in the right place. Inserting a table does not move the cursor, so that's why I ended up using the EndKey property to get the cursor past the table. My code for all this (with changes) is posted. Take a look....I changed a few other things and I added some logic to add header info for each table. There is a good chance I will use this in the future, so I am still playing around with it. One thing I have noticed...It is not as easy to format the way you want as I originally thought....need to learn more about Word VBA!!!. Cheers.

    Attached Files Attached Files
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    That is some really cool stuff there AHARA....!!!

    I actually found the problem with the tables just before your last reply...and made the adjustment....

    I also had to adjust the code around my listviews since I did not ahve them in an array...

    I also was learning a little bit of the Word VBA, (which I never had much use for in the past)....I sure have learned alot...

    Thanks for your dilligence...and hard work...


  11. #11
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    I enjoyed the voyage....It was truly a pleasure (in fact I'm still havin' fun with it!! )

    cheers
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width