【问题标题】:Unable to close Word when there is an instance of Word Application running in my app当我的应用程序中运行 Word 应用程序的实例时无法关闭 Word
【发布时间】:2015-04-12 09:54:38
【问题描述】:

我创建了一个使用Word.Application 打开word 文档的Windows 窗体,代码运行良好,我可以查看、编辑和保存文档。

问题是当我打开另一个 Microsoft Word 实例(在我的应用程序之外)时,单词实例在同一个 WINWORD.exe 进程上打开,我的意思是任务管理器进程中只有一个 WINWORD.exe 进程,无论有多少我打开的单词实例,因此,我的应用程序外部的单词实例无法关闭(使用 X 按钮、文件 => 关闭或 Alt + F4),除非我关闭我的应用程序内的单词实例,否则它无法关闭。

我不知道为什么它在我的应用程序创建的同一进程中打开,它将新打开的单词实例视为我的 Word 应用程序对象的文档(但是它在我的应用程序外部的新单词窗口中打开),所以你不能在不关闭主应用程序对象的情况下关闭文档。

这是我用来在表单中加载单词应用程序的代码:

Public Sub LoadDocument(ByVal t_filename As String)
    filename = t_filename
    If wd Is Nothing Then
        wd = New Word.Application()
        End If

    Try
        wd.CommandBars.AdaptiveMenus = False
    Catch
    End Try

    If wordWnd = 0 Then
        wordWnd = FindWindow("Opusapp", wd.Caption)
    End If
    If wordWnd <> 0 Then
        SetParent(wordWnd, Me.Handle.ToInt32())

        Dim newTemplate As Object = False
        Dim docType As Object = 0
        Dim [readOnly] As Object = True
        Dim isVisible As Object = True
        Dim missing As Object = System.Reflection.Missing.Value

        Try
            If wd Is Nothing Then
                Throw New WordInstanceException()
            End If

            If wd.Documents Is Nothing Then
                Throw New DocumentInstanceException()
            End If

            If wd IsNot Nothing AndAlso wd.Documents IsNot Nothing Then
                'Dim HearingTemplet As Word.Template = wd.NormalTemplate

                Dim d As Word.Documents = wd.Documents

                document = d.Add(fileName, True, docType, isVisible)

                document.ShowGrammaticalErrors = False
                document.ShowRevisions = False
                document.ShowSpellingErrors = False

                document.Application.Visible = False
                document.Application.Options.SuggestSpellingCorrections = False
                document.Application.Options.CheckGrammarAsYouType = False
                document.Application.Options.CheckSpellingAsYouType = False
                document.Application.Options.CheckGrammarWithSpelling = False
                document.SaveAs(fileName)
            End If
        Catch
        End Try

        Try
            wd.ActiveWindow.DisplayRightRuler = False
            wd.ActiveWindow.DisplayScreenTips = False
            wd.ActiveWindow.DisplayVerticalRuler = False
            wd.ActiveWindow.DisplayRightRuler = False
            wd.ActiveWindow.ActivePane.DisplayRulers = False
            wd.ActiveWindow.Caption = ""

            wd.Options.SaveInterval = 0
            wd.Options.AllowFastSave = False
            wd.ActiveWindow.Document.ReadOnlyRecommended = True
            wd.ActiveWindow.Split = False
            wd.Application.DisplayRecentFiles = False
            wd.ActiveWindow.ActivePane.View.Type = WdViewType.wdNormalView
            wd.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView
        Catch
        End Try
        Try
            wd.Visible = True
            wd.Activate()               
        Catch
        End Try
    End If
End Sub

P.S:无论我打开多少个word实例都会出现问题,它们都是在同一个进程上打开的,它们都被视为我的word应用程序的文档,我除非我关闭表单中的 Word 应用程序,否则无法关闭其中任何一个。

【问题讨论】:

    标签: .net vb.net winforms process ms-word


    【解决方案1】:

    您是否在完成后尝试处置对象或退出应用程序?

    wd.quit()
    

    见:https://msdn.microsoft.com/en-us/library/office/ff844895.aspx

    set wd = Nothing
    GC.Collect()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多