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.
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:
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.
"Knowledge is gained when different people look at the same information in different ways"
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.
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:
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"
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:
On Error GoTo Error_Handler
Dim intNumofRows As Integer
Dim intNumofColumns As Integer
Dim p As Integer, ColWidth As Integer
Dim i As Integer
intNumofColumns = m_Recordset.Fields.Count
intNumofRows = m_Recordset.RecordCount
'Insert a new table with rows according to recordCount plus Column header
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:
Select Case mdiMaster.ActiveForm.Name
Case Is = "frmPrfmSmmry"
If ButtonKey = "tbExport" Then
Set wrdDoc = wrdApp.Documents.Add
wrdDoc.Select
Set wrdSelection = wrdApp.Selection
Dim ctl As Control
For Each ctl In frmPrfmSmmry.Controls
If TypeOf ctl Is ListView Then
Set rsQuery = getMultiValues(ctl, "*")
Call InsertTableWithData(rsQuery)
wrdSelection.InsertBreak Type:=wdPageBreak
End If
Next
wrdDoc.SaveAs App.Path & "\Summary.doc"
wrdDoc.Close
MsgBox "File created"
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?????
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.
"Knowledge is gained when different people look at the same information in different ways"