Re: Closing word properly
VB Code:
For each P as Process.GetProcessesByName("WINWORD")
P.CloseMainWindow
Next
Note that this prompts the user to save his work (of he hasnt already done it). The user could press cancel, resulting in Word not closing down.
Re: Closing word properly
Ask the process to exit nicely by calling its CloseMainWindow method first. Only if it hasn't exited after a timeout period, which may indicate that it can't, should you call its Kill method. Search the forum for closemainwindow with my user name and you'll find more than one code example.
Re: Closing word properly
Hello i found this code that you posted but this dosen't close word but will eventually kill it? Anyideas?
Code:
Dim procs As Process() = Process.GetProcessesByName("WINWORD")
For Each proc As Process In procs
' Ask the process to closely gracefully.
proc.CloseMainWindow()
'Wait up to five seconds for the process to exit.
proc.WaitForExit(5000)
If Not proc.HasExited Then
'Force the process to exit.
proc.Kill()
End If
'Free resources.
proc.Close()
Next proc
End Sub
vb.net 2003 code please!
Re: Closing word properly
Actually it does close another instance of Word no worries just not the one within my form. Is this something to do with the fact the label on my form is the handler of word?
Re: Closing word properly
Terminating a process is never a good solution. Since this is Word you could automate it to attachan Application object variable to your instance and then just issue the .Close SaveChanges:=True and a .Quit() so it gracefully closes th open document and THEN quits the application with no issues or corruption of the document.
Re: Closing word properly
Could you give me a code example?
Re: Closing word properly
I'm still working on another code example for another member but using GetObject will allow you to set the object variable to that instance and then its just word automation from there like I mentioned. If you need more help I'll be done witht hte other code in a few minutes.
Re: Closing word properly
Yeah i do if that ok! No hurry.
Re: Closing word properly
Robdogg888 could really do with your help if you don't mind!!
Re: Closing word properly
Try using something like this...
vb Code:
Imports Microsoft.Office.Interop
Public Class Form1
Friend Sub CloseWord()
Dim oApp As Word.Application
Try
oApp = DirectCast(Microsoft.VisualBasic.GetObject(, "Word.Application"), Word.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
MessageBox.Show("Word not found", "VB/Office Guruâ„¢ Word Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show(ex.Message, "VB/Office Guruâ„¢ Word Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
End Try
oApp.Documents.Close
oApp.Quit()
End Sub
End Class
Re: Closing word properly
Should moapp be oapp?
Word.application is not defined is that a 2005 command as im using 2003.
Thanks for your help!
Re: Closing word properly
I updated the posts code. You need to make sure you inported the .Interop class. Word used in that conext is a qualifier for the constant.
Modified from 2003 to work in 2005 but I can post another for 2003
Re: Closing word properly
One other thing, if you are loading Word into a Label then instead of just starting that process you could use Word Automation from the start and you wont need to try to close the process with all this code or end up closing some other word process that was created by the user. ;)
See this part of my Office FAQ: http://vbforums.com/showthread.php?t=406637
Re: Closing word properly
Being a pain now ... It dosen't like Imports Microsoft.Office.Interop it dosen't have office as an option after microsoft.
Ill take a look at your link
Re: Closing word properly
You need to add a reference to Word but then you will be tied in to only one version of support. See the link in my sig for "Primary Interop Assemblies".
To support multiple versions of Word, you need to use late binding (also in that link).