使用 VB.NET 时 Word 在特定模板上冻结

Word Freezes on Specific Template while using VB.NET

所以我一直在使用 MS Word 为我的程序生成报告,并且一切正常,除了在这个模板上它只是在某个点冻结。现在我知道问题出在 Word 模板上,因为我尝试在同一代码中使用另一个(不是我想要的)并且它起作用了。

这是我目前尝试过的列表:

  1. 正在删除模板,使用copy/paste制作新模板进入新模板。
  2. 从头开始制作一个。 (但它只在这个模板上一直冻结。它没有任何异常。我有另一个具有相同表格和数据的模板,但它没有冻结。)
  3. 禁用 MS Word 中的加载项(作为管理员,但没有它)。
  4. 打开并修复模板。

我认为不需要任何代码,但如果有人看到我没有看到的东西,我会 post 我的代码。

感谢任何帮助:)

编辑:

    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

这是我正在使用的代码,....在 IF 语句的 "Arabic" 部分是问题所在,"English" 部分使用了相同的代码但是没有任何问题发生。

文档的英文部分是

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

我在阿拉伯语部分尝试过,一切顺利。

显然,删除这部分:

Try
     appWord.Selection.InsertStyleSeparator()

Catch ex As Exception

End Try

问题已解决。虽然我真的不明白,因为那部分遍布我的代码,而且我在其他任何地方都没有遇到问题:/

谢谢你的帮助:)