I created a windows form that opens a word document using Word.Application
, the code works fine and I am able to view, edit and save the document.
The problem is when I open another Microsoft Word instance (outside my app), the word instance opens on the same WINWORD.exe
process, I mean there is only one WINWORD.exe
process in the task manager processes no matter how many Word instances I open, thus, the word instance that is outside my app cannot be closed (using the X button, File => Close, or Alt + F4), it cannot be closed unless I close the word instance inside my app.
I do not know why it is opening on the same process that my app created, it is treating the newly opened word instance as a document of my Word Application object (however it opens in a new word window outside my app), so you cannot close the document without closing the main app object.
Here is the code that I am using to load the word application inside my form:
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: the problem occurs no matter how many word instances I open, all of them are being opened on the same process, all of them are treated as a document of my word application, and I cannot close any of them unless I close the Word Application in my form.