Zenab Nafez Sammour Zenab Nafez Sammour - 7 months ago 32
Vb.net Question

Word Freezes on Specific Template while using VB.NET

So I've been using MS Word to generate reports for my program, and everything is working fine, except on this one template where it just freezes up at a certain point. Now I know for a fact that the problem is from the Word Template because I tried using another one(Not the one I want) in the same code and it worked.

Here is a list of what I tried so far:


  1. Deleting the template, making a new one using copy/paste into the new one.

  2. Making one from scratch.
    (but it keeps freezing just on this one template. There is nothing out of the normal with it. I have another one that has the same tables and Data in it but it doesn't freeze up.)

  3. Disabling add-ins from MS Word(As administrator and without it).

  4. Open and Repair for the Template.



I don't think any code is needed but if someone sees something I don't I'll post my code.

Any help appreciated :)

Edit:

If My.Settings.Language = "Arabic" Then
Dim appWord As Word.Application = New Microsoft.Office.Interop.Word.Application

If appWord Is Nothing Then
MessageBox.Show("Word is not properly installed!!")
Return
End If

Dim doc As Word.Document

appWord = CreateObject("Word.Application")
appWord.Visible = False

doc = appWord.Documents.Add(Application.StartupPath & "\AncestorsSheetTempArabic2.dotx")

connect()
Dim objConn As OleDbConnection
objConn = acsconn
objConn.Open()


Dim daMain As OleDbDataAdapter
Dim DataSetMain As DataSet
Dim strSQLMain As String

If ID = 0 Or ID = 1 Or ID = vbNull Then
strSQLMain = "SELECT * from People WHERE ((ID=" & IDW & "))"
Else
strSQLMain = "SELECT * from People WHERE ((ID=" & ID & "))"
End If

daMain = New OleDbDataAdapter(strSQLMain, objConn)
DataSetMain = New DataSet
DataSetMain.Clear()
daMain.Fill(DataSetMain, "Main")

With doc
.FormFields("PersonTitle").Result = DataSetMain.Tables("Main").Rows(0).Item("FirstName") & " " & DataSetMain.Tables("Main").Rows(0).Item("LastName")
''''PERSON'''''
.FormFields("PersonsName").Result = DataSetMain.Tables("Main").Rows(0).Item("FirstName") & " " & DataSetMain.Tables("Main").Rows(0).Item("LastName")
If IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("BirthDate")) Then
.FormFields("PersonsBirthDate").Result = "(غير معروف)"
Else
.FormFields("PersonsBirthDate").Result = DataSetMain.Tables("Main").Rows(0).Item("BirthDate")
End If
If DataSetMain.Tables("Main").Rows(0).Item("BirthPlace") = "" Or IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("BirthPlace")) Then
.FormFields("PersonsBirthPlace").Result = "(غير معروف)"
Else
.FormFields("PersonsBirthPlace").Result = DataSetMain.Tables("Main").Rows(0).Item("BirthPlace")
End If

Try
If IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("PictureFile")) Then
Else
Dim bytes As Byte() = (DataSetMain.Tables("Main").Rows(0).Item("PictureFile"))
Dim ms As New MemoryStream(bytes)
Dim img As Image = Image.FromStream(ms)
img = FixedSize(img, 100, 100)

Dim ms2 As New MemoryStream
img.Save(GetDesktop() & "\TempImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

.InlineShapes.AddPicture(GetDesktop() & "\TempImage.jpg").Select()
appWord.Selection.Cut()
.Tables(3).Cell(1, 1).Select()
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdChartPicture)
appWord.Selection.Cut()
.Tables(3).Cell(1, 1).Select()
appWord.Selection.Paste()
My.Computer.FileSystem.DeleteFile(GetDesktop() & "\TempImage.jpg")

End If
Catch ex As Exception

End Try
.FormFields("PersonsNameEvents").Result = DataSetMain.Tables("Main").Rows(0).Item("FirstName") & " " & DataSetMain.Tables("Main").Rows(0).Item("LastName")

If IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("CurrentEducationLevel")) Then
Else
.FormFields("EventsName").Result = "التعليم: "
.FormFields("EventsDisc").Result = DataSetMain.Tables("Main").Rows(0).Item("CurrentEducationLevel")
.Tables(3).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If DataSetMain.Tables("Main").Rows(0).Item("Nationality") = "" Or IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("Nationality")) Then
Else
.FormFields("EventsName").Result = "الجنسية: "
.FormFields("EventsDisc").Result = DataSetMain.Tables("Main").Rows(0).Item("Nationality")
.Tables(3).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If DataSetMain.Tables("Main").Rows(0).Item("CurrentJob") = "" Or IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("CurrentJob")) Then
Else
.FormFields("EventsName").Result = "المهنة: "
.FormFields("EventsDisc").Result = DataSetMain.Tables("Main").Rows(0).Item("CurrentJob")
.Tables(3).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If DataSetMain.Tables("Main").Rows(0).Item("Religion") = "" Or IsDBNull(DataSetMain.Tables("Main").Rows(0).Item("Religion")) Then
Else
.FormFields("EventsName").Result = "الديانة: "
.FormFields("EventsDisc").Result = DataSetMain.Tables("Main").Rows(0).Item("Religion")
.Tables(3).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If .FormFields("EventsName").Result = "" And .FormFields("EventsDisc").Result = "" Then
.Tables(3).Rows(3).Select() 'Select the table
appWord.Selection.Delete()
.Tables(3).Rows(2).Select() 'Select the table
appWord.Selection.Delete()
End If
'daKids.Dispose()
'''''''FATHER''''''
Dim daFatherRela As OleDbDataAdapter
Dim DataSetFatherRela As DataSet
Dim strSQLFatherRela As String

strSQLFatherRela = "SELECT * from Relationship WHERE ((Person2ID=" & DataSetMain.Tables("Main").Rows(0).Item("ID") & " and RelationshipeTypeCode=2))"

daFatherRela = New OleDbDataAdapter(strSQLFatherRela, objConn)
DataSetFatherRela = New DataSet
DataSetFatherRela.Clear()
daFatherRela.Fill(DataSetFatherRela, "FatherRela")

Dim daFather As OleDbDataAdapter
Dim DataSetFather As DataSet
Dim strSQLFather As String
Dim FathersID As String

For i = 0 To DataSetFatherRela.Tables("FatherRela").Rows.Count - 1
strSQLFather = "SELECT * from People WHERE ((ID=" & DataSetFatherRela.Tables("FatherRela").Rows(i).Item("Person1ID") & "))"
FathersID = DataSetFatherRela.Tables("FatherRela").Rows(i).Item("Person1ID").ToString
daFather = New OleDbDataAdapter(strSQLFather, objConn)
DataSetFather = New DataSet
DataSetFather.Clear()
daFather.Fill(DataSetFather, "Father")
If DataSetFather.Tables("Father").Rows.Count > 0 Then
.FormFields("PNum").Result = .Tables.Count - 4
.FormFields("PName").Result = DataSetFather.Tables("Father").Rows(0).Item("FirstName") & " " & DataSetFather.Tables("Father").Rows(0).Item("LastName")
If IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("BirthDate")) Then
.FormFields("PDOB").Result = "(غير معروف)"
Else
.FormFields("PDOB").Result = DataSetFather.Tables("Father").Rows(0).Item("BirthDate")
End If
If DataSetFather.Tables("Father").Rows(0).Item("BirthPlace") = "" Or IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("BirthPlace")) Then
.FormFields("PPOB").Result = "(غير معروف)"
Else
.FormFields("PPOB").Result = DataSetFather.Tables("Father").Rows(0).Item("BirthPlace")
End If


Try
If IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("PictureFile")) Then
Else
Dim bytes As Byte() = (DataSetFather.Tables("Father").Rows(0).Item("PictureFile"))
Dim ms As New MemoryStream(bytes)
Dim img As Image = Image.FromStream(ms)
img = FixedSize(img, 100, 100)

Dim ms2 As New MemoryStream
img.Save(GetDesktop() & "\TempImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

.InlineShapes.AddPicture(GetDesktop() & "\TempImage.jpg").Select()
appWord.Selection.Cut()
.Tables(5).Cell(1, 1).Select()
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdChartPicture)
appWord.Selection.Cut()
.Tables(5).Cell(1, 1).Select()
appWord.Selection.Paste()
My.Computer.FileSystem.DeleteFile(GetDesktop() & "\TempImage.jpg")

End If
Catch ex As Exception

End Try

.FormFields("PNameEvents").Result = DataSetFather.Tables("Father").Rows(0).Item("FirstName") & " " & DataSetFather.Tables("Father").Rows(0).Item("LastName")

If IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("CurrentEducationLevel")) Then
Else
.FormFields("PEventsName").Result = "التعليم: "
.FormFields("PEventsDisc").Result = DataSetFather.Tables("Father").Rows(0).Item("CurrentEducationLevel")
.Tables(5).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("Nationality")) Then
Else
.FormFields("PEventsName").Result = "الجنسية: "
.FormFields("PEventsDisc").Result = DataSetFather.Tables("Father").Rows(0).Item("Nationality")
.Tables(5).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("CurrentJob")) Then
Else
.FormFields("PEventsName").Result = "المهنة: "
.FormFields("PEventsDisc").Result = DataSetFather.Tables("Father").Rows(0).Item("CurrentJob")
.Tables(5).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
If IsDBNull(DataSetFather.Tables("Father").Rows(0).Item("Religion")) Then
Else
.FormFields("PEventsName").Result = "الديانة: "
.FormFields("PEventsDisc").Result = DataSetFather.Tables("Father").Rows(0).Item("Religion")
.Tables(5).Rows(3).Select() 'Select the table
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1)
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
End If
'If .FormFields("EventsName").Result = "" And .FormFields("EventsDisc").Result = "" Then
' .Tables(5).Rows(3).Select() 'Select the table
' appWord.Selection.Delete()
' .Tables(3).Rows(2).Select() 'Select the table
' appWord.Selection.Delete()
'End If
.Tables(5).Select()
appWord.Selection.CopyAsPicture() 'Copy the table
appWord.Selection.MoveDown(Word.WdUnits.wdParagraph, 1) 'moves after the table
'appWord.Selection.MoveDown(Word.WdUnits.wdScreen, 2)
'appWord.DefaultTableSeparator = Environment.NewLine & "-"
Try
appWord.Selection.InsertStyleSeparator()

Catch ex As Exception

End Try
appWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault) 'pastes the original table(template)
Try
appWord.Selection.InsertStyleSeparator()
Catch ex As Exception

End Try
'''''''''''''''''''''''''''' After this line nothing works '''''''''''''''''''''''''''''''''''
.Tables(6).Split(.Tables(6).Rows.Count) '''''''''''''''''''''''
End If

Next
'''''''''''''''''''''' After this is the same code as the father but for finding the Mother and grandparents ... etc ... I can't add them because that is too much characters.
objConn.Close()
End With
appWord.Visible = True

ElseIf My.Settings.Language = "English" Then

End If


This is the code I'm using, .... In the "Arabic" Part of the IF statement is where the problem is , the same code is used in the "English" part but nothing wrong happens.

In the english part the document is

doc = appWord.Documents.Add(Application.StartupPath & "\AncestorsSheetTemp.dotx")


I tried that in the Arabic part and everything went fine.

Answer

So apparently, removing this part :

Try
     appWord.Selection.InsertStyleSeparator()

Catch ex As Exception

End Try

Solved the issue. Though I really don't understand since that part is all over my code and I haven't gotten a problem with it anywhere else :/

Thanks for the help though :)