0

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.

  • Is Word visible while you're building your report? Possibly make it visible to see if it is showing any warning. – Andrew Mortimer May 04 '16 at 10:01
  • I tried that. It just says (Not Responding) at the top. – Zenab Nafez Sammour May 04 '16 at 10:18
  • Can you add the line of code where the lockup occurs, and the data you're using, into the question? – Andrew Mortimer May 04 '16 at 10:22
  • I added it, But note that I put that Line into a comment and reran the program, it just gave me the next line (When I'm searching for the Mother) as an issue, I put that into a comment and it gave me the next line. – Zenab Nafez Sammour May 04 '16 at 10:48
  • It doesn't give an actual error, it just stops at the line when i pause the program when word freezes – Zenab Nafez Sammour May 04 '16 at 10:49
  • How many rows in the table? Try adding a thread.sleep(100) after the line. – dbasnett May 04 '16 at 11:56
  • This isn't directly an answer, but it will show a bit of info about "Never use 2 dots on the same line". http://stackoverflow.com/questions/2511464/winword-exe-wont-quit-after-calling-word-documents-add-word-net-interop – Andrew Mortimer May 04 '16 at 11:56
  • One other thought, publish your app, install it, and run it. I have interop apps that don't work so well in the ide but run just fine if the are installed. – dbasnett May 04 '16 at 12:25
  • The table has 4 or more rows(I dynamically add rows trough the code). @AndrewMortimer that really doesn't solve anything but thanks :) I tried that but no luck either :/ .. I'm pretty sure it has sth to do with the word template, not the program though – Zenab Nafez Sammour May 04 '16 at 12:43
  • It isn't intended as a solution, just extra info about how unpredictable interop seems to get. How about copying and renaming the working template to the name of the failing template? – Andrew Mortimer May 04 '16 at 12:53
  • I'm not completely clear on where the freeze is occurring: Right from the start of the code you included? Is there a reason you start two instances of the Word.Application, assigning both to the same object? That seems suspect to me. First you use `= New Word.Application" and then you use `appWord = CreateObject`. Try commenting that second one out - the first should be enough. – Cindy Meister May 04 '16 at 17:17

1 Answers1

0

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 :)