|
-
Apr 14th, 2007, 09:47 AM
#1
Thread Starter
Member
Closing word properly
Im running word within a label on my form and i want to be able to close word properly upon form closing so that when the program is re run you don't get document recovery coming up. Im currently using the following but this always causes document recovery to appear the next time it is run. Thanks
Code:
For Each currentProcess As Process In killword
If currentProcess.ProcessName = "WINWORD" Then currentProcess.Kill()
Next
Using vb.net 2003
-
Apr 14th, 2007, 09:52 AM
#2
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.
-
Apr 14th, 2007, 09:59 AM
#3
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.
-
Apr 14th, 2007, 12:40 PM
#4
Thread Starter
Member
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!
-
Apr 14th, 2007, 01:36 PM
#5
Thread Starter
Member
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?
-
Apr 15th, 2007, 01:55 PM
#6
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 15th, 2007, 02:13 PM
#7
Thread Starter
Member
Re: Closing word properly
Could you give me a code example?
-
Apr 15th, 2007, 02:20 PM
#8
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 15th, 2007, 02:48 PM
#9
Thread Starter
Member
Re: Closing word properly
Yeah i do if that ok! No hurry.
-
Apr 20th, 2007, 03:51 AM
#10
Thread Starter
Member
Re: Closing word properly
Robdogg888 could really do with your help if you don't mind!!
-
Apr 20th, 2007, 03:58 AM
#11
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
Last edited by RobDog888; Apr 20th, 2007 at 04:16 AM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 20th, 2007, 04:12 AM
#12
Thread Starter
Member
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!
-
Apr 20th, 2007, 04:17 AM
#13
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 20th, 2007, 04:29 AM
#14
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 20th, 2007, 05:08 AM
#15
Thread Starter
Member
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
-
Apr 20th, 2007, 12:39 PM
#16
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).
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|