Free Support Forum - aspose.com

Error trying to save word file after using document builder

Hello,

I am still testing your ASPOSE.Words component.

Now I got another problem.

After saving my document using ASPOSE.Words I can not open the document and save it again.

Opening the document takes a lot of time and trying to save document fails.

CPU 99 % and does not stop.

Even only opening and saving the file does not work.

Enclosed please find the code.

Can you please tell me if there is something wrong ??

Is something wrong with my document ?

Can you please help me ?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim MyRuntime As Date

myruntime = now()

OpenDBConnection()

CreateTableDoc()

CloseDBConnection()

msgbox(now().Subtract(MyRuntime).ToString)

End Sub

Sub OpenDBConnection()

Const SQL_SRV = "."

Const SQL_DB = "MyDB"

Const SQL_USER = "MyUSR"

Const SQL_PWD = "MyPWD"

Conn = New SqlClient.SqlConnection

Conn.ConnectionString = " User ID = " & SQL_USER & _

"; Password = " & SQL_PWD & _

"; Initial Catalog = " & SQL_DB & _

"; Data Source = " & SQL_SRV & _

"; Connect Timeout = 10" & _

"; Packet Size = 8192" & _

"; Pooling = True" & _

"; Connection LIfeTime = 10" & _

"; Min Pool Size = 1" & _

"; Max Pool Size = 5"

Conn.Open()

End Sub

Sub CloseDBConnection()

Conn.Close()

Conn = Nothing

End Sub

Sub CreateTableDoc()

Dim myDoc As Aspose.Words.Document

Dim myBuilder As Aspose.Words.DocumentBuilder

Dim myFileName As String

Dim myText As String

Dim i As Int32

' Open the template document

myDoc = New Document("C:\temp\ASPOSE\STL.dot")

myBuilder = New DocumentBuilder(myDoc)

FillField(myBuilder, "KdNr", "00007")

FillField(myBuilder, "EmpfText1", "text 1")

FillField(myBuilder, "EmpfText2", "text 2")

FillField(myBuilder, "EmpfText3", "text 3")

FillField(myBuilder, "EmpfText4", "text 4")

FillField(myBuilder, "EmpfText5", "text 5")

FillField(myBuilder, "EmpfText6", "text 6")

FillField(myBuilder, "EmpfText7", "text 7")

FillField(myBuilder, "EmpfText8", "text 8")

FillField(myBuilder, "EmpfText9", "text 9")

FillField(myBuilder, "EmpfName1", "text")

FillField(myBuilder, "EmpfName2", "text")

FillField(myBuilder, "EmpfStrasse", "text")

FillField(myBuilder, "EmpfPLZ", "text")

FillField(myBuilder, "EmpfOrt", "text")

FillField(myBuilder, "EmpfTel", "text")

FillField(myBuilder, "DokKey", "text")

FillField(myBuilder, "KDNR", "text")

FillTable(myBuilder, "mytable")

myDoc.MailMerge.DeleteFields()

myBuilder = Nothing

myDoc.save("C:\Temp\ASPOSE\MyDoc.doc")

mydoc = Nothing

End Sub

Sub FillField(byRef myBuilder as Aspose.Words.DocumentBuilder, myFldName As String, myFldText As String)

myBuilder.MoveToMergeField(myFldName)

myBuilder.Write(myFldText)

End Sub

Sub FillTable(byRef myBuilder, myFldName)

Dim MyTable As DataTable

Dim MyRow As DataRow

Dim MyVals(3) As String

myBuilder.MoveToMergeField(myFldname)

mySQL = "SELECT TOP 4 a, b, c, d " & _

"FROM ABC"

Erg = PCSQLGetDataTable(MySQL, MyTable, Conn, 0)

mybuilder.CellFormat.Borders.LineStyle = LineStyle.Single

mybuilder.CellFormat.Borders.Color = system.Drawing.Color.Black

mybuilder.CellFormat.Borders.LineWidth = 0.5

mybuilder.CellFormat.TopPadding = 2

mybuilder.CellFormat.BottomPadding = 2

mybuilder.StartTable()

InsertHeader(myBuilder, MyTable)

For Each MyRow In MyTable.Rows

InsertRow(myBuilder, myRow)

Next

mybuilder.EndTable

End Sub

Private sub InsertHeader(byref mybuilder as Aspose.Words.DocumentBuilder, mytable As DataTable)

Dim i As Int32

for i = 0 To mytable.Columns.Count - 1

mybuilder.CellFormat.Shading.BackgroundPatternColor = system.Drawing.Color.LightGray

mybuilder.Font.Bold = True

mybuilder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center

mybuilder.CellFormat.Width = 100

mybuilder.InsertCell()

mybuilder.Write(mytable.Columns(i).Caption)

Next

mybuilder.EndRow()

End Sub

Private sub InsertRow(byref mybuilder as Aspose.Words.DocumentBuilder, myrow As DataRow)

Dim i As Int32

for i = 0 To myrow.ItemArray.GetUpperBound(0)

mybuilder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center

mybuilder.Font.Bold = False

mybuilder.CellFormat.Shading.BackgroundPatternColor = system.Drawing.Color.White

mybuilder.InsertCell()

mybuilder.Write(myrow.ItemArray(i))

Next

myBuilder.EndRow

End Sub

Regards,

Manuela

Please send us the problem document before and after being asposed.