MS Office Forum / Word / Programming / May 2008
appending word doc
|
|
Thread rating:  |
jishith - 20 May 2008 06:51 GMT hello,
1) How to open adoc file in append mode?
What i want is to save and close the word doc which i created using VB. then i want to open it again and continue from where i stopped.Why i doing these because the word doc i creating is a huge one so after 20mb size the application fail and popping the error " command failed". When i tried with small data the application works fine. If the file size is huge the error pops up.
These is the Code i used to fetch data from temp table and printing in word doc.
Private Function CreateSubWordReport(subDoc As Word.Document, strTestScriptName As String, lngTestScriptID As Long, oConnection As ADODB.Connection) As Boolean Dim rng As Word.Range Dim rs1 As ADODB.Recordset Dim rs2 As ADODB.Recordset Dim tbl As Word.Table Dim expectedResultsFlag As Byte Dim rowCount As Long Dim testcaseCount As Long Dim eventCount As Integer Dim subtblFAIL As Word.Table Dim subrowCountFAIL As Long Dim subtblHWFAIL As Word.Table Dim subrowCountHWFAIL As Long Dim subtblSWFAIL As Word.Table Dim subrowCountSWFAIL As Long Dim subtblSpecFAIL As Word.Table Dim subrowCountSpecFAIL As Long Dim subtblBenchFAIL As Word.Table Dim subrowCountBenchFAIL As Long Dim subadr As String Dim flagTestEventFail As Boolean eventCount = 0 testcaseCount = 0 'Set up connecton to the SQL Server Dim oConnection_SQLserver As ADODB.Connection Set oConnection_SQLserver = New ADODB.Connection oConnection_SQLserver.ConnectionString = ConnectionString_SQLServer
Set rs1 = New ADODB.Recordset Set rs2 = New ADODB.Recordset 'Set rng equal to the range of the sub word report Set rng = subDoc.Range(Start:=0, End:=subDoc.Range.End)
rng.SetRange subDoc.Range.End, subDoc.Range.End 'Print the TestCasetName at the End of the document rng.InsertParagraphAfter rng.InsertParagraphAfter rng.SetRange rng.End, rng.End rng.Style = wdStyleHeading3 rng.InsertAfter (" " & "Test Cases") 'Select all the testcases for the above testscript rs1.Open "SELECT TestCaseName, FinalStatus, TestCaseTime FROM [Temp_Testcases] WHERE TestScriptID = " & lngTestScriptID & " ORDER BY TestCaseTime", oConnection, adOpenStatic, adLockReadOnly 'Repeat for all Testcases Do While (rs1.EOF = False) ActiveDocument.SpellingChecked = True ActiveDocument.GrammarChecked = True 'Print the testcase name rng.InsertParagraphAfter rng.InsertParagraphAfter rng.SetRange rng.End, rng.End rng.Style = wdStyleHeading4 rng.InsertBefore (" " & rs1(0).Value) rng.InsertParagraphAfter rng.InsertParagraphAfter rng.SetRange rng.End, rng.End
'Select all the Testevents belonging to the above testcase whose logtype is not equal to 5 oConnection_SQLserver.Open rs2.Open "SELECT logTime, logType, Comment1,Comment2,logStatus FROM TestEvent WHERE TestScriptID = " & lngTestScriptID & " AND TestCaseTime = " & rs1(2).Value & " AND logType <> 5 ORDER BY logTime", oConnection_SQLserver, adOpenStatic, adLockReadOnly If (rs2.EOF = True) Then GoTo ENDOFLOOP End If
'Add a table to print the testevents. rng.Tables.Add Range:=rng, NumRows:=2, NumColumns:=3 'enter the first row in the table Set tbl = rng.Tables(1) tbl.Cell(1, 1).Range.Text = "Action" tbl.Cell(1, 2).Range.Text = "Results" tbl.Cell(1, 3).Range.Text = "Test Result"
'expected Results flag is set to 1 on receiving a testevent with logstatus equal to 1 or 3 expectedResultsFlag = 0 rowCount = 2 flagTestEventFail = False 'repeat for all testevents in each selected testcase Do While (rs2.EOF = False) 'move to next row when expectedResultsFlag = 1 If (expectedResultsFlag = 1 And (rs2.Fields(1) = 0 Or rs2.Fields(1) = 2)) Then tbl.Rows.Add rowCount = rowCount + 1 expectedResultsFlag = 0 flagTestEventFail = False End If 'change the color to RED only if the testevent is Fail else leave it as Black, which is default If (rs2.Fields(4) = 0) Then 'case Fail tbl.Cell(rowCount, 1).Range.Font.Color = wdColorRed tbl.Cell(rowCount, 2).Range.Font.Color = wdColorRed flagTestEventFail = True Else If (rs2.Fields(4) = 1 And flagTestEventFail = False) Then 'case Pass tbl.Cell(rowCount, 1).Range.Font.Color = wdColorBlack tbl.Cell(rowCount, 2).Range.Font.Color = wdColorBlack flagTestEventFail = True End If End If 'CHECK THE LOGSTATUS FIELD
If (rs2.Fields(1) = 0 Or rs2.Fields(1) = 2) Then 'enter the testevent under the "Actions" column in the table tbl.Cell(rowCount, 1).Range.InsertAfter (rs2.Fields(2) & " " & rs2.Fields(3)) tbl.Cell(rowCount, 1).Range.InsertParagraphAfter tbl.Cell(rowCount, 1).Range.SetRange tbl.Cell(rowCount, 1).Range.End, tbl.Cell(rowCount, 1).Range.End ElseIf (rs2.Fields(1) = 1 Or rs2.Fields(1) = 3) Then 'enter the testevent under the "Expected Results" column in the table tbl.Cell(rowCount, 2).Range.InsertAfter (rs2.Fields(2) & " " & rs2.Fields(3)) tbl.Cell(rowCount, 2).Range.InsertParagraphAfter tbl.Cell(rowCount, 2).Range.SetRange tbl.Cell(rowCount, 2).Range.End, tbl.Cell(rowCount, 2).Range.End expectedResultsFlag = 1 End If
rs2.MoveNext 'save the document from time to time eventCount = eventCount + 1 If (eventCount > 1000) Then subDoc.Save eventCount = 0 End If 'transfer control to the OS DoEvents
Loop tbl.Borders.InsideLineStyle = True tbl.Borders.OutsideLineStyle = True rng.SetRange tbl.Range.End, tbl.Range.End rng.InsertParagraphBefore rng.SetRange rng.End, rng.End 'increment the testcase count testcaseCount = testcaseCount + 1 Forms![Progress Status].ProgressBar.SetFocus
ENDOFLOOP: rs2.Close oConnection_SQLserver.Close rs1.MoveNext Loop rs1.Close For Each toc In ActiveDocument.TablesOfContents toc.Update Next subDoc.Save 'Cancel has NOT been pressed in the progress window CreateSubWordReport = False End Function
the code which comes under Check for log field,( which i wrote in Caps) is continuosly fetching data from temp table and printing to word report. So after creating 1185 pages the "command failed " error pop ups. Want to know what the cause of the error and how to resolve it.
Friend i pretty tnew to VBA. so finds difficult to trace the error. please give the solution .
Jean-Guy Marcil - 20 May 2008 13:10 GMT > hello, > [quoted text clipped - 9 lines] > These is the Code i used to fetch data from temp table and printing in word > doc. <...snip...>
> the code which comes under Check for log field,( which i wrote in Caps) is > continuosly fetching data from temp table and printing to word report. So > after creating 1185 pages the "command failed " error pop ups. Want to know > what the cause of the error and how to resolve it. When writing code that performs lots of operations on a document, the Undo "stack" becomes too large for Word, it is a good idea to clear it once in a while.
You can add
subDoc.UndoClear
before your DoEvents line.
But, how many rows does your table have when you reach 1,185 pages? Word has more difficulties handling one large table than many small tables...
jishith - 21 May 2008 08:14 GMT Hello Jean, thanks for your reply. its almost having 3500's of pages. main problem is it taking almost 9 hours to create word doc. have any way to reduce the timing.
> > hello, > > [quoted text clipped - 29 lines] > But, how many rows does your table have when you reach 1,185 pages? > Word has more difficulties handling one large table than many small tables... Jean-Guy Marcil - 21 May 2008 13:47 GMT > Hello Jean, > thanks for your reply. its almost having 3500's of pages. main problem is it > taking almost 9 hours to create word doc. have any way to reduce the timing. A 3,500-page document consisting of one table? How many rows do you end up with? If less than 65,000, you should probably use Excel instead...
It may make things run faster if you create a regular doucment instead, using a tab or some other unique character to separate each field on a line, and then, at the end, convert the whole document to a table. Also, make sure you turn repagination off, working in drat view will help for that.
But, as I wrote earlier, Word has more difficulty handling one lare table than many small ones, especially such a monster of a table as the one you are creating over 3,500 pages...
scorpion53061 - 21 May 2008 15:26 GMT > Hello Jean, > thanks for your reply. its almost having 3500's of pages. main problem is it [quoted text clipped - 35 lines] > > - Show quoted text - http://www.word.mvps.org/FAQs/TblsFldsFms/FastTables.htm
Jean-Guy Marcil - 21 May 2008 17:10 GMT > http://www.word.mvps.org/FAQs/TblsFldsFms/FastTables.htm Ah, yes, excellent. I had forgotten abut that tremendously useful article...
jishith - 22 May 2008 06:23 GMT Hello jean, Sorry jean, not 3500 pages, its 3500 tables. the table may contain min 3 rows max depend on data. but it won't exeed 10 rows but may contain some amount of lines, which may take pages.
> > http://www.word.mvps.org/FAQs/TblsFldsFms/FastTables.htm > > Ah, yes, excellent. I had forgotten abut that tremendously useful article... Jean-Guy Marcil - 22 May 2008 13:11 GMT > Hello jean, > Sorry jean, not 3500 pages, its 3500 tables. the table may contain min 3 > rows max depend on data. but it won't exeed 10 rows but may contain some > amount of lines, which may take pages. With 3,500 tables on 1,185 pages you may have reached some limit...
Try the tips on the page scorpion53061 pointed out. If none of that helps, try saving, closing and reopening the doucment every 500 or 250 tables you create with your code. If all that fails, consider making smaller documents..., joining them after the code has finished running, either manually or with another routine
If none of the above work, then I am sorry, I don't know what else to write!
Good luck wiht this humongous-document-creation project!
jishith - 23 May 2008 08:37 GMT For reopen , i want to get it in append mode right. what to know how to open that in append mode?
> > Hello jean, > > Sorry jean, not 3500 pages, its 3500 tables. the table may contain min 3 [quoted text clipped - 12 lines] > > Good luck wiht this humongous-document-creation project! Jean-Guy Marcil - 23 May 2008 12:59 GMT > For reopen , i want to get it in append mode right. what to know how to open > that in append mode? I am not sure what you mean. There is no "append mode" when opening a Word document.
Just set a Range object to the and of the document and use that to start "appending" stuff to the end of the document... For example, this code will "append" a new paragraph with the text "New Appended Text" at the end of the current document:
Dim rngAppend As Range Dim strText As String
strText = "New Appended Text"
Set rngAppend = ActiveDocument.Range
With rngAppend .InsertParagraphAfter .Collapse wdCollapseEnd .InsertAfter strText End With
|
|
|