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 .